【发布时间】:2020-06-04 22:03:47
【问题描述】:
我在 .csv 文件中有以下数据:
1,-100,phrase1
2,-100,phrase2
3,1,phrase3
4,-100,phrase4
5,1,phrase5
当找到 -100 时,我想将所有“短语*”添加到一个字符串中,并用逗号分隔直到最后一个条目。到目前为止,我有以下内容:
import csv
output = []
with open('test_file.csv') as test_file:
csv_reader_object = csv.reader(test_file)
for expression_id, expression_weight, expression_phrase in csv_reader_object:
expression_weight = int(expression_weight)
if expression_weight == -100:
output.append(expression_phrase)
print(output)
我在想我可以遍历列表,找到 -100,将 expression_phrase 添加到列表中,然后遍历列表并将所有表达式短语添加到一个大字符串中。我正在寻找以下输出:
字符串 = (phrase1,phrase2,phrase4)
感谢任何帮助/提示。我一直在想办法解决这个问题。
【问题讨论】: