循环浏览csv文件中以列表中的元素开头的列中的关键字列表-Python
看来我没能正确理解这件事。我有一个通过csv文件循环的关键字列表。所有这些关键字都位于第二列中循环浏览csv文件中以列表中的元素开头的列中的关键字列表-Python,python,python-3.x,Python,Python 3.x,看来我没能正确理解这件事。我有一个通过csv文件循环的关键字列表。所有这些关键字都位于第二列中 keywords_list = ['account number','venmo cashout','quickpay'] 这些关键字只是银行对账单中每笔交易的开始,我需要捕捉每笔交易都是从这些关键字开始的。以下是我迄今为止尝试的代码: input_file = '/Users/.../input_file.csv' output_file = '/Users/.../output_file.csv
keywords_list = ['account number','venmo cashout','quickpay']
这些关键字只是银行对账单中每笔交易的开始,我需要捕捉每笔交易都是从这些关键字开始的。以下是我迄今为止尝试的代码:
input_file = '/Users/.../input_file.csv'
output_file = '/Users/.../output_file.csv'
final_output_list = []
with open(input_file,'r') as raw_data_opener:
key_words_search = csv.reader(raw_data_opener, delimiter=',')
for j, col in enumerate(key_words_search):
for i in range(len(keywords_list[i]):
if col[1].str.startswith(keywords_list[i]): <----something wrong here
final_output_list.append(col)
with open(output_file, 'w', newline='') as outfile:
writer=csv.writer(outfile)
for row in final_output_list:
writer.writerow(row)
print('Finished...')
所需输出:对于以关键字列表开头的每个行项目
Date Description Amount
01/02 Venmo Cashout.. xxx
通过删除
.str
将解决此问题
if col[1].startswith(key_words_list[i]):
它将返回以关键字列表中的元素开头的行项目欢迎使用StackOverflow。看见在您发布MRE代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您指定的问题。还包括调试尝试。“猜猜……有什么不对劲”不是正确的诊断。如果没有其他内容,请插入两条有用的
print
语句来检查您正在测试的值。@Prune抱歉。vs代码控制台未返回任何问题,且输出文件中未写入任何行项目。@Prune请添加input\u文件的内容示例和所需输出示例。“如果没有其他内容,请插入两条有用的print
语句以检查您正在测试的值。”你试过了吗?代码的工作原理是将.str
从OP。。。谢谢大家。。
Date Description Amount
01/02 Venmo Cashout.. xxx