简体   繁体   中英

Python read lines from input file and write back to the file

I want to read the input file, line by line, then modify one line and write back the changes to the same file

the problem is that, after writing back, I lose the return to the line and I have all the data in one line

open(bludescFilePath, 'a+') as blu:

blu_file_in_lines = blu.readlines()

for line in blu_file_in_lines:
                    if "Length" in line:
                        blu_file_in_lines[13] = line.replace("0x8000",str(size))

 with open(bludescFilePath, 'w') as blu:
                blu.write(str(blu_file_in_lines))

EDIT

Ok, what was missing is the for loop.

with open(bludescFilePath, 'w') as blu:
    for line in blu_file_in_lines:
        blu.write(str(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