list - Python Iterate over a numbers in a txt file -
i have txt file similar following
example.txt
9987.000 2300 23 40 30 9654.988 1234 34 32 19
i iterate on file , rewrite file sample.txt follows each number on own line listed vertically in order in listed horizontally.
9987.000 2300 23 40 30
i new python , not sure best approach doing this. advice appreciated. syntax error
file "sample.py", line 64 open('testfile.txt')as infile, open('testfile.txt','w') outfile: ^ syntaxerror: invalid syntax
for large files:
with open('path/to/input') infile, open('path/to/output', 'w') outfile: line in infile: num in line.strip().split(): outfile.write(num + '\n')
for small files:
import itertools open('path/to/input') infile, open('path/to/output', 'w') outfile: outfile.write('\n'.join(itertools.chain.from_iterable(line.strip().split() line in infile)))
Comments
Post a Comment