scott.turner
scott.turner

Reputation: 89

searching for a value in csv and returning the row multiple times

i'm a noob trying to learn python,

i am trying to write a script for a CSV file that has 30,000 rows of data.

i would like to look through every row for a number in a column and return the row every time it finds that number.

i have searched and tried many different suggestion and they don't seem to do what i need it to can anyone help me, if i'm not making sense please let me know.

here is what i have so far and it is only returning to headers:

    import csv

with open("test.csv", "r") as input, open ("result.txt","w") as result:
          testfilereader = csv.DictReader(input) 
          Age = 23
          fieldnames = testfilereader.fieldnames
          testfilewriter = csv.DictWriter(result, fieldnames, delimiter=',',)
          testfilewriter.writeheader()      
          for row in testfilereader:
                 for field in row:
                        if field == Age:
                             testfilewriter(row)


input.close

thanks all

Upvotes: 4

Views: 17873

Answers (4)

scott.turner
scott.turner

Reputation: 89

hi all thank you for all your posts i have had some trouble with my computer and installing panda so i had to try another way and this has worked for me.

import csv
import sys


number = '5' 

csv_file = csv.reader(open('Alerts.csv', "rb"), delimiter=",")
filename  = open("Result.txt",'w')
sys.stdout =filename

#loop through csv list
for row in csv_file:
    if number == row[0]:
       print row

Upvotes: 0

fatpiratecat
fatpiratecat

Reputation: 1

Since you used DictFileReader, you get a list of dictionaries. so you should search for the age in the field you want by using dictionary['field']. like this:

with open("test.csv", "r") as input, open ("result.txt","w") as result:
     testfilereader = csv.DictReader(input) 
     Age = 23
     fieldnames = testfilereader.fieldnames()
     testfilewriter = csv.DictWriter(result, fieldnames, delimiter=',',)
     testfilewriter.writeheader()      
     for row in testfilereader:
         if row['Age'] == Age:
            testfilewriter.writerow(row)

Of course, if the field name is something else you need to change row['Age'] to row['Somethingelse'].

If you just want ot iterate over the values you should use testfilereader.values(), but then there would be no point in getting the data mapped to a dictionary in the fist place.

You also shouldn't try to close the input there. It will be closed when it leaves the with open... block.

Upvotes: 0

Sebastian Wozny
Sebastian Wozny

Reputation: 17506

You can use the pandas module which is made for processing tabular data.

First: read your csv into a so called DataFrame:

import pandas as pd
df = pd.read_csv("test.csv")

Now you can filter the rows that you need by logical indexing:

result = df[df['Age']==23]

To get the result back onto disk just use the to_csv method:

result.to_csv('result.csv')

Upvotes: 3

Carles Mitjans
Carles Mitjans

Reputation: 4866

You can use Pandas as follows:

csv file:

Id,Name,Age
1,John,30
2,Alex,20
3,Albert,30
4,Richard,30
5,Mariah,30

python:

import pandas as pd

df = pd.read_csv("ex.csv", sep = ",")
print df[df["Age"] == 30] 

   Id     Name  Age
0   1     John   30
2   3   Albert   30
3   4  Richard   30
4   5   Mariah   30

Upvotes: 5

Related Questions