CSV Search Question

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 8
Reputation: betatype is an unknown quantity at this point 
Solved Threads: 0
betatype betatype is offline Offline
Newbie Poster

CSV Search Question

 
0
  #1
May 27th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: CSV Search Question

 
0
  #2
May 27th, 2009
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):

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: betatype is an unknown quantity at this point 
Solved Threads: 0
betatype betatype is offline Offline
Newbie Poster

Re: CSV Search Question

 
0
  #3
May 29th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC