简体   繁体   English

python readlines() 只看到一行

[英]python readlines() sees only one line

I want to write simple parser for simple file.我想为简单文件编写简单的解析器。 Here is file which I want to parse:这是我要解析的文件:

"""{\"dlugosc\":44.41207996473956,\"szerokosc\":5.0144114626069447}"""
"""{\"dlugosc\":33.41207996473956,\"szerokosc\":5.0144114626069447}"""
"""{\"dlugosc\":22.41207996473956,\"szerokosc\":5.0144114626069447}"""
"""{\"dlugosc\":11.41207996473956,\"szerokosc\":5.0144114626069447}"""
...
...

It's just simple text file nothing else.它只是简单的文本文件,没有别的。

Here is my 'parser' code:这是我的“解析器”代码:

with open('file.txt', 'r+') as file:
    content = file.readlines()
    for line in content:
        line = line.replace('\\', "").replace('"', "").replace('{', "").replace('}', '')

with open('file.txt', 'a') as appending:
    appending.write('\n'+line)

And strange output after run:运行后奇怪的输出:

"""{\"dlugosc\":44.41207996473956,\"szerokosc\":5.0144114626069447}"""
"""{\"dlugosc\":33.41207996473956,\"szerokosc\":5.0144114626069447}"""
"""{\"dlugosc\":22.41207996473956,\"szerokosc\":5.0144114626069447}"""
"""{\"dlugosc\":11.41207996473956,\"szerokosc\":5.0144114626069447}"""
dlugosc:11.41207996473956,szerokosc:5.0144114626069447

I ofcourse know that python replace method doesn't work on same declared string but making it's copy as new object, so as you can see I've made this line - line = line.replace('\\\\', "").replace('"', "").replace('{', "").replace('}', '') .我当然知道 python 替换方法不适用于同一个声明的字符串,而是将其复制为新对象,因此您可以看到我已经创建了这一行 - line = line.replace('\\\\', "").replace('"', "").replace('{', "").replace('}', '')

My question is why output looks like this above - only last line was parsed?我的问题是为什么输出看起来像上面这样 - 只有最后一行被解析? I know also about this a appending arg and that's what I want to achieve - old lines should stay in file, but I want all of them to be parsed also.我也知道这a附加的 arg,这就是我想要实现的 - 旧行应该保留在文件中,但我希望它们也都被解析。

Thanks!谢谢!

Your loops and file writing aren't nested the way you think they are;您的循环和文件写入并没有像您认为的那样嵌套; when you write:当你写:

with open('file.txt', 'a') as appending:
    appending.write('\n'+line)

...this is outside of the for line in content loop, so there's only one line to write! ...这for line in content循环中的for line in content之外,所以只有line要写!

Instead, try something like:相反,请尝试以下操作:

with open('file.txt', 'r+') as file:
    content = file.readlines()
    
    # at this point, the stream should be at the end of the file, 
    # so it's safe to write for appending
    for line in content:
        line = line.replace('\\', "").replace('"', "").replace('{', "").replace('}', '')
        file.write("\n" + line)

...though I really wouldn't recommend reading to and writing from the same file as a pattern; ...虽然我真的不建议从同一个文件中读取和写入作为模式; generally you want to keep the original file just in case you need to re-create your data transformation from scratch通常,您希望保留原始文件,以防万一您需要从头开始重新创建数据转换

Because in this code block, you only write one line.因为在这个代码块中,你只写了一行。

with open('file.txt', 'a') as appending:
    appending.write('\n'+line)

You can store the lines to be appended in a list and append them seperately.您可以将要追加的行存储在列表中并单独追加。

result = []

with open('file.txt', 'r+') as file:
    content = file.readlines()
    for line in content:
        result.append(line.replace('\\', "").replace('"', "").replace('{', "").replace('}', ''))

with open('file.txt', 'a') as appending:
    appending.write('\n')
    for line in result:
        appending.write(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM