简体   繁体   English

使用Python在大型CSV文件中进行关键字搜索

[英]Keyword search in a large CSV file using Python

I wonder if there is a way to search a list of keywords using Python. 我想知道是否有一种使用Python搜索关键字列表的方法。 I am able to search though my data using PostgreSQL. 我可以使用PostgreSQL搜索数据。 Here is my PostgreSQL code 这是我的PostgreSQL代码

SELECT distinct ON (id) id, year, cost, description
FROM mydata
WHERE description similar to'%((hotel)||(travel)|(taxi)|(food))%';

I don't know if Python is the best way to do it, but the work that I am replicating uses Python and would like to stick with Python. 我不知道Python是否是最好的方法,但是我正在复制的工作使用Python,并且希望坚持使用Python。

I was able to search one keyword but am not sure how to do multiple. 我能够搜索一个关键字,但不确定如何做多个。

 import csv
 with open('mydata.csv', 'r') as f:
    for line in f.readlines():
        if 'food' in line:
            print(line)

I need help with 我需要帮助

1) searching using multiple keywords 1)使用多个关键字进行搜索

2) way to export the data back to csv 2)将数据导出回csv的方法

> import pandas as pd

multiple keyword search using contains 使用包含的多关键字搜索

> df = pd.read_csv('mydata.csv')
> df[df.description.str.contains('hotel|travel|taxi|food')]

Export to csv 导出到CSV

> df.to_csv('new.csv')

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

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