Wednesday 15 July 2015

python - Copying a range of columns from a text file and creating a new file -



python - Copying a range of columns from a text file and creating a new file -

i new python , doing info parsing sonar files have more info need process data. have found code , trying manipulate meet goal. need first 53 columns of info lastly 100+ columns extraneous info not needed.

f = open("adv125cm.txt", "r") g = open("adv125cm_fixed.txt", "w") line in f: if line.strip(): g.write("\t".join(line.split(0,53)[1:]) + "\n") f.close() g.close()

i got error code , have no thought means:

typeerror: expected character buffer object

any help improve no help.

you typeerror: expected character buffer object because string.split expects string of chars stripped. passing 0, 53. confused piece operation. if want first 53 columns of line need line[:53]. applying alter for loop code becomes:

for line in f: if line.strip(): g.write("\t".join(line[:53]) + "\n")

however if want first 53 columns of every line leading , trailing whitespace characters stripped, code refactored this:

for line in f: g.write(line.strip()[:53] + '\n')

python parsing text-files

No comments:

Post a Comment