简体   繁体   中英

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('}', '') .

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.

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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