Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to write a python script which reads data from one csv file and based on that data it picks up the data from another csv file and writes it in third csv. However, the csv.reader() doesn't seem to work. It always returns
<_csv.reader object at 0x7f1d7150ce50>
<_csv.reader object="" at="" 0x7f1d714c8b40="">


I tried using fseek(0) also. But even that doesn't work. What am I doing wrong?
Here's the entire code.
Python
__author__ = 'sudarshini'
import csv, os
pattern="new"
good=[]
i=0
j=0
for path, dirs, files in os.walk("/home/sudarshini/Downloads/train"):
        for filename in files:
                if pattern in filename:
                        good[:]=[]
                        with open(filename,'r') as f:
                                f.seek(0)
                                print f
                                reader1=csv.reader(f)
                                lol=list(reader1)
                                pattern2 = lol[1]
                                actual=pattern2[0]
                                oho=actual[0:14]
                                print oho
                                print reader1
                                i=0
                                for path, dirs, files in os.walk("/home/sudarshini/Downloads/train"):
                                        for fa in files:
                                                if oho in fa:
                                                        with open(fa,'r') as f2:
                                                                f2.seek(0)
                                                                reader2=csv.reader(f2)
                                                                print reader2
                                                                for row in reader1:
                                                                        flag=0
                                                                        print "HOHO"
                                                                        for row2 in reader2:
                                                                                if(i==0):
                                                                                        good.append(row2)
                                                                                        i=i+1
                                                                                        break

                                                                                if(row[0]==row2[0]):
                                                                                        print row[0]
                                                                                        flag=1
                                                                                        break

                                                                        if(flag==1):
                                                                                good.append(row2)

                        with open('/home/sudarshini/Downloads/train/final_%i.csv' %j,'w') as f3:
                                j=j+1
                                writer=csv.writer(f3)
Posted
Updated 20-Jul-15 4:44am
v2
Comments
Richard MacCutchan 20-Jul-15 11:06am    
Take a look at https://docs.python.org/2/library/csv.html, and try a simple sample, rather than the somewhat complicated code you have posted above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900