Urvi Soni
Urvi Soni

Reputation: 314

Extracting Rows by specific keyword in Python (Without using Pandas)

My csv file looks like this:-

ID,Product,Price

1,Milk,20
2,Bottle,200
3,Mobile,258963
4,Milk,24
5,Mobile,10000

My code of extracting row is as follow :-

def search_data():

    fin = open('Products/data.csv')
    word = input() # "Milk"
    found = {}
    for line in fin:

        if word in line:

            found[word]=line

    return found

search_data()

While I run this above code I got output as :-

{'Milk': '1,Milk ,20\n'}

I want If I search for "Milk" I will get all the rows which is having "Milk" as Product.

Note:- Do this in only Python don't use Pandas

Expected output should be like this:-

[{"ID": "1", "Product": "Milk ", "Price": "20"},{"ID": "4", "Product": "Milk ", "Price": "24"}]

Can anyone tell me where am I doing wrong ?

Upvotes: 0

Views: 864

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

In your script every time you assign found[word]=line it will overwrite the value that was before it. Better approach is load all the data and then do filtering:

If file.csv contains:

ID     Product     Price

1      Milk        20
2      Bottle      200
3      Mobile      10,000
4      Milk        24
5      Mobile      15,000

Then this script:

#load data:
with open('file.csv', 'r') as f_in:
    lines =  [line.split() for line in map(str.strip, f_in) if line]
    data = [dict(zip(lines[0], l)) for l in lines[1:]]

# print only items with 'Product': 'Milk'
print([i for i in data if i['Product'] == 'Milk'])

Prints only items with Product == Milk:

[{'ID': '1', 'Product': 'Milk', 'Price': '20'}, {'ID': '4', 'Product': 'Milk', 'Price': '24'}]

EDIT: If your data are separated by commas (,), you can use csv module to read it:

File.csv contains:

ID,Product,Price

1,Milk ,20
2,Bottle,200
3,Mobile,258963
4,Milk ,24
5,Mobile,10000

Then the script:

import csv

#load data:
with open('file.csv', 'r') as f_in:
    csvreader = csv.reader(f_in, delimiter=',', quotechar='"')
    lines = [line for line in csvreader if line]
    data = [dict(zip(lines[0], l)) for l in lines[1:]]

# # print only items with 'Product': 'Milk'
print([i for i in data if i['Product'].strip() == 'Milk'])

Prints:

[{'ID': '1', 'Product': 'Milk ', 'Price': '20'}, {'ID': '4', 'Product': 'Milk ', 'Price': '24'}]

Upvotes: 1

Related Questions