在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()
你在这里所做的对我来说似乎是正确的,但你只是修改了加载到内存中的文件的内容,而不是磁盘上的文件。你知道吗
在处理来自的行时,需要将每一行写入一个新文件投影.txt 比如:
您可以使用
re
:输出:
你的企图毫无意义。修改行时,修改将丢失。你知道吗
以读/写方式写入文本文件是行不通的。在文件的某处写入
false
也无法工作。。。你知道吗我的建议是:
csv
模块来处理拆分像这样:
相关问题 更多 >
编程相关推荐