Python替换特定lin中的字符串

2024-10-02 02:39:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: 文件thetxtfalsetruecloseaswith
3条回答

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

在处理来自的行时,需要将每一行写入一个新文件投影.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")

您可以使用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

你的企图毫无意义。修改行时,修改将丢失。你知道吗

以读/写方式写入文本文件是行不通的。在文件的某处写入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)

相关问题 更多 >

    热门问题