繁体   English   中英

在python的文本文件中搜索CSV列表文件

[英]Search a CSV list file in a text file in python

如何在 python 的文本文件中搜索单个原始 CSV 文件(包含很多单词的列表)。 或者只是我只想找到一个单词列表并想知道它们是否存在于我的文本文件中。 我怎么能在python中做到这一点? 或者,如果我可以使用特定的应用程序来做到这一点,请告诉我,我想在 excel 中尝试,但它无法导入 CSV 或预先搜索的单词列表。 先感谢您

  1. 这个程序将一个单词列表作为输入,它们之间有空格,并检查它们是否在 CSV 文件中。
  2. 如果是,程序将它们添加到列表中并打印它们。

不要忘记更改 CSV 文件的名称。

#!/usr/bin/python
import csv
from queue import Empty
import sys

#input the list of words you want to search and split words by SPACE
input_string = input('Enter elements of a list separated by space ')
string_list  = input_string.split()

#read csv, and split on "," the line
csv_file = csv.reader(open('convertcsv.csv', "r"), delimiter=",")

total = set()

#loop through the csv list
for row in csv_file:
    for field in row:
        total.add(field)

list=[]

#add the matches to a list
for input in string_list:
    if input in total:  
        if input not in list:
            list+=[input]


#print the list
print(list)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM