简体   繁体   中英

searching keyword in text file and printing the entire row

ok so i used an api and arranged a text file that looks like this:

TITLE,YEAR,IMDB
'Money Plane','2000','tt7286966'
'Mulan','2020','tt4566758'
'Secret Society of Second Born Royals','2020','tt10324122'
'Train to Busan Presents: Peninsula', 'year': '2020', 'imdb_id': 'tt8850222'
'After We Collided','2020','tt10362466'
'One Night in Bangkok','2020','tt12192190'
'Phineas and Ferb The Movie: Candace Against the Universe','2020','tt1817232'
'Cats & Dogs 3: Paws Unite','2020','tt12745164'
'The Crimes That Bind','2020','tt10915060'
'Scoob!','2020','tt3152592'
'We Bare Bears: The Movie','2020','tt10474606'
'Trolls World Tour', 'year': '2020', 'imdb_id': 'tt6587640'
'Birds of Prey (and the Fantabulous Emancipation of One Harley Quinn)','2020','tt7713068'
'Bad Boys for Life','2020','tt1502397'
'Greyhound','2020','tt6048922'
'The Old Guard','2020','tt7556122'
'Sonic the Hedgehog','2020','tt3794354'
'Dad Wanted','2020','tt12721188'
'Barbie: Princess Adventure','2020','tt12767498'
                            

now i want to be able so search a movie by either title,year or id

bassicaly i want to make it so that when i search for a title, it will show me the entire details about the movie, example: search for mulan, and my output will be:

'Mulan','2020','tt4566758'

my code so far looks like this:

import pandas


data=pandas.read_csv(r"C:\Users\Home\Documents\studying\newproject\moviedata.txt")
title=list(data["TITLE"])
year=list(data["YEAR"])
imdbid=list(data["IMDB"])

ive tried to use

for t,y,imdb in zip(title,year,imdbid):
   if word in title:
      items=data[word]
      print(items)

it runs but it does nothing

You can use here numpy.where to find value and pandas loc to print row

Code:

import pandas as pd
import numpy as np

df = pd.read_csv('moviedata.txt')

row , col = np.where(df == "'Mulan'")
print(df.loc[row])

Output:

    TITLE   YEAR    IMDB
1   'Mulan' '2020'  'tt4566758'

If you want output in form of list

row , col = np.where(df == "'Mulan'")
a = df.loc[row]
a = a.values.tolist()
print(a)

Output:

[["'Mulan'", "'2020'", "'tt4566758'"]]

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