我的文本文件如下所示:
Fjodor Dostojevski|Zlocin i kazna|1866|9788897572657|roman|4500|50|true|
Dragoslav Mihajlovic|Kad su cvetale tikve|1968|3332221115554|drama|3000|20|true|
Viliam Sekspir|Hamlet|1600|3214569879878|drama|6500|60|true|
我需要搜索9788897572657
或3332221115554
之类的数字,如果该数字符合排名,则将该字词true
更改为false
。
答案 0 :(得分:1)
以下是一些处理您的行的代码。对于数据中的每一行,它将:
|
set
false
。join
<强>代码:强>
# build a set of the keys we will look for
looking_for = {"9788897572657", "3332221115554"}
output_data = []
for line in test_data:
fields = line.split('|')
if fields[3] in looking_for:
fields[7] = 'false'
output_data.append('|'.join(fields))
for line in output_data:
print(line)
测试数据:
test_data = [x.strip() for x in """
Fjodor Dostojevski|Zlocin i kazna|1866|9788897572657|roman|4500|50|true|
Dragoslav Mihajlovic|Kad su cvetale tikve|1968|3332221115554|drama|3000|20|true|
Viliam Sekspir|Hamlet|1600|3214569879878|drama|6500|60|true|
""".split('\n')[1:-1]]
<强>产地:强>
Fjodor Dostojevski|Zlocin i kazna|1866|9788897572657|roman|4500|50|false|
Dragoslav Mihajlovic|Kad su cvetale tikve|1968|3332221115554|drama|3000|20|false|
Viliam Sekspir|Hamlet|1600|3214569879878|drama|6500|60|true|