【问题标题】:Python Replace string in certain linePython替换某行中的字符串
【发布时间】:2018-01-31 15:16:32
【问题描述】:

在txt文件projections.txt这样

0001|AA|17.12.2017.|20:30|21:00|ponedeljak|For a few dolars more|150|true
0002|BB|19.12.2017.|19:30|21:15|sreda|March on the Drina|300|true
0003|GG|20.12.2017.|18:00|19:00|cetvrtak|A fistful of Dolars|500|true
0004|GG|21.12.2017.|21:15|00:00|petak|The Good, the Bad and the Ugly|350|true

如何在某行中将字符串“true”更改为“false”(删除)。 例如,如果我输入 0002,如何在第 2 行更改。 我试过这样的东西,但它不起作用......

def delete_projection():
    with open('projections.txt') as projections:
    #projections = open('users.txt', 'r').readlines()
        delete = input("Input projection code you want to delete: ")
        for i in projections:
            projection = i.strip("\n").split("|")
            if delete == projection[0]:
                projection[8]="false"
                #projections.close()
    with open('projections.txt', 'w+') as projections:
        projections.write("false")
        projections.close()

【问题讨论】:

  • 您必须以 csv 格式读取文件,修改并写入完整文件。在文本文件上没有读/写之类的东西。*
  • 顺便说一句,您的尝试在许多方面都毫无意义。

标签: python string file text


【解决方案1】:

你的尝试毫无意义。当您修改行时,修改会丢失。

并且无法以读/写方式写入文本文件。在文件某处写入false 也无法正常工作...

我的建议:

  • 使用csv模块处理拆分
  • 将行作为行列表读取
  • 存储修改后的行
  • 用新数据覆盖文件

像这样:

import csv

def delete_projection():
    with open('projections.txt') as projections:
    #projections = open('users.txt', 'r').readlines()
        delete = input("Input projection code you want to delete: ")
        contents = []
        cr = csv.reader(projections,delimiter="|")
        for row in cr:
            if delete == row[0]:
                row[8]="false"
            contents.append(row)

    with open('projections.txt', 'w',newline="") as projections:
        cw = csv.writer(projections,delimiter="|")
        cw.writerows(contents)

【讨论】:

  • 将整个文件内容保存在内存中可能很麻烦。
  • 它有效。只需将“users.txt”更改为“projections.txt”。我将其编辑为。
【解决方案2】:

你可以使用re:

import re
s = """
0001|AA|17.12.2017.|20:30|21:00|ponedeljak|For a few dolars more|150|true
0002|BB|19.12.2017.|19:30|21:15|sreda|March on the Drina|300|true
0003|GG|20.12.2017.|18:00|19:00|cetvrtak|A fistful of Dolars|500|true
0004|GG|21.12.2017.|21:15|00:00|petak|The Good, the Bad and the Ugly|350|true
""" 
vals = re.findall('(?<=\n)\d+(?=\|)|(?<=^)\d+(?=\|)', s)
statements = re.findall('true|false', s)
new_s = re.sub('true|false', '{}', s)
to_change = '0002'
s = new_s.format(*[('true' if a == 'false' else 'false') if b == to_change else a for a, b in zip(statements, vals)])

输出:

0001|AA|17.12.2017.|20:30|21:00|ponedeljak|For a few dolars more|150|true
0002|BB|19.12.2017.|19:30|21:15|sreda|March on the Drina|300|false
0003|GG|20.12.2017.|18:00|19:00|cetvrtak|A fistful of Dolars|500|true
0004|GG|21.12.2017.|21:15|00:00|petak|The Good, the Bad and the Ugly|350|true

【讨论】:

    【解决方案3】:

    你在这里所做的对我来说似乎是正确的,但你只是修改了加载到内存中的文件的内容,而不是磁盘上的文件。

    您需要将每一行写入一个新文件,因为您正在处理来自 projection.txt 的行 比如:

    with open('projections.txt') as projections:
        with open('projections-new.txt') as projections_new:
            delete = input("Input projection code you want to delete: ")
            for i in projections:
                projection = i.strip("\n").split("|")
                if delete == projection[0]:
                   projection[8]="false"
                projections_new.write("|".join(projection) + "\n")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 2012-02-18
      • 1970-01-01
      • 2019-10-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多