简体   繁体   English

在 Python 的 csv 文件中搜索单词/短语

[英]Search for a word/phrase in csv file in Python

I have a database of tweets in csv format which looks like this - screen of csv database - and I need to perform the following task with this file using Python code:我有一个 csv 格式的推文数据库,看起来像这样 - csv 数据库的屏幕- 我需要使用 Python 代码对该文件执行以下任务:

Search for certain words/phrases in tweets (text of a tweet is in the column C) and if the tweet has this word/phrase I'm looking for, I need to write the whole row with this tweet to a new csv file在推文中搜索某些词/短语(推文的文本在 C 列中),如果推文中有我要查找的这个词/短语,我需要将包含这条推文的整行写入新的 csv 文件

and (if possible) to delete this tweet from the old csv file or create a new one without it.并且(如果可能)从旧的 csv 文件中删除这条推文或创建一个没有它的新推文。

I hope I made it clear.我希望我说清楚了。

You can use the csv library to read the file and do your search on each row one at a time.您可以使用 csv 库读取文件并一次对每一行进行搜索。

import csv

with open('out.csv', 'w', newline='') as output_file:
    writer = csv.writer(output_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    with open('test.csv') as csv_file:
        csv_read = csv.reader(csv_file, delimiter=',')
        for row in csv_read:
            if "a" in row[2]:
                writer.writerow(row)

Here is a link to the python docs: https://docs.python.org/3/library/csv.html .这是 python 文档的链接: https ://docs.python.org/3/library/csv.html。 Hope this helps.希望这可以帮助。

EDIT: If you want more than one search term, use any() on a list comprehension.编辑:如果您想要多个搜索词,请在列表理解中使用 any() 。

import csv

with open('out.csv', 'w', newline='') as output_file:
    writer = csv.writer(output_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    with open('input.csv') as csv_file:
        csv_read = csv.reader(csv_file, delimiter=',')
        for row in csv_read:
            search_terms = ["term1", "term2"]

            if any([term in row[2] for term in search_terms]):
                writer.writerow(row)

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

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