| | |
CSV Search Question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary directory drive dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib





