簡體   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