简体   繁体   中英

Keyword search in a large CSV file using Python

I wonder if there is a way to search a list of keywords using Python. I am able to search though my data using PostgreSQL. Here is my PostgreSQL code

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.

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

2) way to export the data back to 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

> df.to_csv('new.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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