Python - 读取、解析和写回文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14089083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - reading, parsing and writing back to a file
提问by Balachander Ramachandran
I am trying to read around 5000 lines of code from a text file then parse and store the parsed values into another text file.
我试图从一个文本文件中读取大约 5000 行代码,然后解析并将解析的值存储到另一个文本文件中。
However, I am able to only do this for the first 967 lines (as indicated by the output file, which contains the parsed values of the first 967 lines only).
但是,我只能对前 967 行执行此操作(如输出文件所示,该文件仅包含前 967 行的解析值)。
Here is the simple code i wrote to do this job.
这是我为完成这项工作而编写的简单代码。
infile = open("Input.txt", "r")
outfile = open("Output.txt", "w")
for line in infile.readline():
temp = infile.readline()
value = temp.split("<_|_>")
outfile.write(value[1])
How would I go about writing the other 4000-odd values?
我将如何编写其他 4000 多个值?
采纳答案by Dolda2000
Because your foriteration is not over the lines in infile, but rather over the characters in its first line. Probably, the first line is 967 characters long.
因为您的for迭代不是在 中的行上infile,而是在其第一行中的字符上。第一行大概有 967 个字符长。
for line in infile.readline()means that you're reading the first line from infileas a string. Strings are iterable, in the way that iterating over them iterates over the characters in that string. Therefore, you're running the loop each time for every character in that line.
for line in infile.readline()意味着您正在从infile字符串中读取第一行。字符串是可迭代的,就像迭代它们会迭代该字符串中的字符一样。因此,您每次都为该行中的每个字符运行循环。
What you want to do is probably, rather, something like this:
你想要做的可能是这样的:
with open("Input.txt", "r") as infile, open("Output.txt", "w") as outfile:
for line in infile:
outfile.write(line.split("<_|_>")[0])
File objects in Python are also iterable. Iterating over them iterates over each line. You coulduse for line in infile.readlines()(Note: readlines, not readline) instead, but that would read all the lines in infileinto one huge array before iterating. Iterating directly on the file object only keeps the latest read line in memory at one time.
Python 中的文件对象也是可迭代的。迭代它们会迭代每一行。您可以使用for line in infile.readlines()(Note: readlines, not readline) 代替,但这会infile在迭代之前将所有行读入一个巨大的数组中。直接在文件对象上迭代一次只会将最新的读取行保留在内存中。
回答by Akhi
You should read each line and parse. you are doing a for loop on a single line in the input file. A simple approach would be to use the while loop
你应该阅读每一行并解析。您正在输入文件的单行上执行 for 循环。一个简单的方法是使用 while 循环
f1 = open('Input.txt','r')
f2 = open('Output.txt','w')
line = f1.readline()
while line:
value = line.split('<_|_>')
f2.write(value[0])
line = f1.readline()
回答by From Gaul
why cant we just use
为什么我们不能使用
f1 = open('Input.txt','r')
f2 = open('Output.txt','w')
for i in f1:
f2.writelines(i)
f2.close()

