I have a bunch of numbers that are tab-delimited with new line characters that looks something like this:
104 109 105 110 126 119 97 103\n
114 129 119 130 122 106 117 128\n
and so on. How can I use python to write all these numbers to a file in one column? i.e.
104\n
109\n
105\n
110\n
126\n
and so on. Fairly new to python so开发者_C百科 any help is appreciated, thanks!
The easiest way to do this is probably to use sed
. But if you must use Python, you need to replace all tab characters with newlines. Try something like this:
with open('input_file', 'rb') as infile:
with open('output_file', 'wb') as outfile:
for line in infile:
outfile.write(line.replace('\t', '\n'))
"\n".join("104 109 105 110 126 119 97 103\n 114 129 119 130 122 106 117 128\n".split())
There are multiple ways of tackling this problem. You could use string.split
and string.join
, but that seems inefficient, since you'd be converting a string into a tuple and back into a string.
Using regex, we replace one or more whitespace characters with a newline. The metacharacter \s
represents any whitespace character), which in Python 2.7.1 is equivalent to [ \t\n\r\f\v]
(and possibly additional whitespace characters, if UNICODE is set).
import re
input_file = open('input_filename','r')
output_file = open('output_filename', 'w')
for line in input_file:
output_file.write(re.sub('[\s]+','\n', line))
input_file.close()
output_file.close()
If your file is small, you can use file.readlines()
to read all the lines into memory:
with open('input.txt', 'r') as fin:
for row in [l.split() for l in fin.readlines()]:
for col in row:
print col
If the file is very large, read the lines into memory one at a time (I like to use a generator):
for row in open('input.txt'):
for col in row.split():
print col
In either case you can pipe the output to a new file:
python myscript.py >output.txt
Replace input_filename and output_filename with appropriate values.
f = open('input_filename','r')
nums = f.read().split()
f.close()
f = open('output_filename', 'w')
f.write('\n'.join(nums))
f.close()
[Edit] Reworked example that doesn't load the whole file into memory. It is now very similar to Chinmay Kanchi's example. But I use split where he uses replace.
with open('input_filename','r') as input:
with open('output_filename', 'w') as output:
for line in input:
output.write('\n'.join(line.split()))
output.write('\n')
精彩评论