【问题标题】:Python read lines from input file and write back to the filePython从输入文件中读取行并写回文件
【发布时间】:2019-01-30 17:41:46
【问题描述】:

我想逐行读取输入文件,然后修改一行并将更改写回同一个文件

问题是,写回后,我失去了返回到该行,而我将所有数据都放在了一行中

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))

【问题讨论】:

标签: python


【解决方案1】:

编辑

好的,缺少的是 for 循环。

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

【讨论】:

    猜你喜欢
    • 2018-08-07
    • 2016-03-20
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多