| | |
CSV Search Question
![]() |
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jun 2008
Posts: 122
Reputation:
Solved Threads: 30
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):
The code goes something like this (not tested):
python Syntax (Toggle Plain Text)
import csv urlReader = csv.reader(open('url.csv'), delimiter=' ', quotechar='|')#FIXME filename delimiter quote etc... url=dict() for row in urlReader: url[row[0]]=row[1] largeReader=csv.reader(open('large.csv'), delimiter=' ', quotechar='|')#FIXME filename delimiter quote etc... largeWriter=csv.writer(open('large_out.csv',"w"), delimiter=' ', quotechar='|')#FIXME filename delimiter quote etc... for row in largeReader: largeWriter.writerow(row+[url.get(row[???]),""]#FIXME ???=title column index
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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)
import csv smallFile = csv.reader(open('/home/user/Desktop/smallfile.csv'), delimiter=';') bigFile = csv.reader(open('/home/user/Desktop/bigfile.csv'), delimiter=';') url=dict() for row in smallFile: url[row[0]]=row[1] for row in bigFile: i=0 result=row[i] if result in url: print result+";"+url[result] else: print result+";"+"no corresponding url" i+=1
![]() |
Similar Threads
- Parsing CSV with php (PHP)
- Fixed width to csv conversion (Python)
- Simple search question (MySQL)
- Vista Search question (Windows Vista and Windows 7)
- a very simple csv reader question (C#)
- Needing Help With MMORPG's (C++)
- svchost.exe (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: .app execution problem
- Next Thread: how to create a web site using python lang.
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically event events examples external file float format frange function google gui hints homework i/o iframe import info input jaunty java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython





