python csv manipulation

Status
Not open for further replies.

redman06

Smack-Fu Master, in training
75
Hey Guys

Im trying to learn python by doing some csv manipulation by looping through 1 CSV then searching another for the matches then picking up a row then paste the matching row back into the first

Can anyone point me in the right direction to whats going wrong? I think its breaking at line 19?

Any help would be appreciated!

Code:
import csv

madison = open("madison.csv","r", encoding='utf8')
csvReader = csv.reader(madison)
price = {}
for row in csvReader:
    if row[12].strip() not in price:
        price[row[12].strip()] = row[4].strip()
madison.close()

products = open("products.csv","r", encoding='utf8')
csvReader = csv.reader(products)
products_data = []
for row in csvReader:
    products_data.append(row)
products.close()

for row in products_data:
    if(row[13].strip() in price):
        row[23] = price[row[13].strip()]
products.close()
products_file = open("products.csv","w",newline='',encoding='utf8')
csvWriter = csv.writer(products_file)
for row in products_data:
    csvWriter.writerow(row)
products_file.close()
 

Chipko

Ars Scholae Palatinae
975
Subscriptor
What is the error you're getting? it's really hard to help without the .csv files. can you post an example of what they look like?

Add a print statement after the madison.close() line to have a look at what you've actually saved in your price dictionary.

it's also good practice to open file using the with statement:

Code:
>>> with open('workfile', 'r') as f:
...     read_data = f.read()

This ensure that the file is closed if an error occurs when the file is open.

Please also be aware that that opening a file in write mode in
Code:
products_file = open("products.csv","w",newline='',encoding='utf8')
will erase all data in products.csv. maybe this is what you want but i'd recommend to use different file name for the output, atleast during development.
 

Alamout

Ars Legatus Legionis
27,285
[url=http:/civis/viewtopic.php?p=30455079#p30455079:35lr4ezh said:
Shuffy[/url]":35lr4ezh]Probably not answering the question, but would it be feasible to roll out your own CSV reader? CSV is a really easy format to parse.
It is, but there's no reason at all to do so. Whatever the problem is, it's almost certainly not in the csv module.

It's very difficult to know the problem without getting more detailed information, though. "I think it's breaking at line 19" isn't very helpful--is that because there's an error message pointing to that line? What is that error message?
 

herko

Tone down Your Control issues.
7,012
Moderator
Things that may break CSV readers, depending on the phase of the moon/tides/wishes of the insane monkey god:
  • Text that should be quoted, but isn't, and contains a comma
  • Paragraphs of text inside a single cell with newlines in it. Even properly quoted, it drivers some CSV readers (like Excel's) insane.
  • Non-ASCII characters
  • Nested quoting
  • Having Excel participate anywhere in the process
  • Having Excel installed on the same computer
  • Having Excel installed on the same network

Jokes aside, csv.reader (and my favorite, csv.DictReader) are stellar. You may need to tune it for the particular dialect of csv your file is written in. See Dialects and Formatting parameters.

CSV is an informal spec at best, and everyone implements it slightly differently. Rolling a csv reader to fit your exact format is trivial. Rolling a csv reader that tolerates the inconsistencies that exist out in the real world... isn't.

If you can post the offending .csv file, we might be able to help further.
 
Status
Not open for further replies.