943,918 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1280
  • Python RSS
May 27th, 2009
0

CSV Search Question

Expand Post »
I have a problem that I've been puzzling with so I thought it was time to ask the experts.

I have two csv files that I am trying to search between. One csv has a Title column, and a URL column. The second has a variety of columns but a Title column that matches the Title column of the first CSV. I want to search the larger CSV to see if a record exists for a particular title and if so append the corresponding URL into a column at the end of the sheet.

I have been browsing the various csv reader properties but I can't seem to find too many examples. Would I use the find function along with csv reader to accomplish this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
betatype is offline Offline
8 posts
since Jan 2009
May 27th, 2009
0

Re: CSV Search Question

I would read in the first csv into a dictionary with title- url key-value pairs. Then go through the second large one line by line, and append the looked up values, and write it to a new file.
The code goes something like this (not tested):

python Syntax (Toggle Plain Text)
  1. import csv
  2. urlReader = csv.reader(open('url.csv'), delimiter=' ', quotechar='|')#FIXME filename delimiter quote etc...
  3. url=dict()
  4. for row in urlReader:
  5. url[row[0]]=row[1]
  6. largeReader=csv.reader(open('large.csv'), delimiter=' ', quotechar='|')#FIXME filename delimiter quote etc...
  7. largeWriter=csv.writer(open('large_out.csv',"w"),
  8. delimiter=' ', quotechar='|')#FIXME filename delimiter quote etc...
  9. for row in largeReader:
  10. largeWriter.writerow(row+[url.get(row[???]),""]#FIXME ???=title column index
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
May 29th, 2009
0

Re: CSV Search Question

The dictionary suggestion was exactly what I needed. I've included the script that I got to work for me. This takes two semicolon delimited files in the format discussed above. It then searches a value from the larger csv against a smaller file with just a "Title" column and a "URL" column.

Python Syntax (Toggle Plain Text)
  1. import csv
  2. smallFile = csv.reader(open('/home/user/Desktop/smallfile.csv'), delimiter=';')
  3. bigFile = csv.reader(open('/home/user/Desktop/bigfile.csv'), delimiter=';')
  4. url=dict()
  5. for row in smallFile:
  6. url[row[0]]=row[1]
  7. for row in bigFile:
  8. i=0
  9. result=row[i]
  10. if result in url:
  11. print result+";"+url[result]
  12. else:
  13. print result+";"+"no corresponding url"
  14. i+=1
Reputation Points: 10
Solved Threads: 0
Newbie Poster
betatype is offline Offline
8 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: .app execution problem
Next Thread in Python Forum Timeline: how to create a web site using python lang.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC