0

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

1

1 Answer 1

0
> 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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.