1

I am trying to solve this problem. I am reading data from csv fiile which has the following columns:

id, name, price

So i use the following code to read the csv:

import sys
import csv as input

def readFile(path):
    try:
        finput = input.reader(open(path,'rb'),delimiter=',',quotechar='|')
    except IOError as (errno,strerror):
            print "I/O error({0}): {1}".format(errno,strerror)
    except:
            print "Unexpected Error: ",sys.exc_info()[0]
            raise
    # covert format into list
    fmod = list(finput)
    return fmod

but the problem is the name field can be like

name, item_det now that " , " creates a trouble for me.. instead of reading the name field as a single entity having a comma in the description.. it splits that particular field. How do I solve this. Thanks

3
  • 2
    Read the file in a line at a time (no delimiter) and look at one of the python regex functions with more advanced features to split the string into fields Commented Nov 3, 2011 at 22:11
  • 3
    Normally, your CSV file would then have quotes in it, so that a line would be |name, item_det|, other_field, another_field (since you set quotechar='|'). The CSV module can handle that. If not, you'll probably want to do like @MartinBeckett said and parse it manually, or check the length of the list and merge those two fields manually if necessary. Commented Nov 3, 2011 at 22:19
  • do you have a control over the csv input file creation or you are forced to deal with broken csv files? usually people use quotechar='"' Commented Nov 3, 2011 at 22:28

2 Answers 2

1

CSV is exactly that: "Comma Separated". You either need to quote the name field:

|name,item_det|

Or use an escape character, but you have to turn it on by setting quoting to QUOTE_NONE:

reader = csv.reader(open(path, "rb"), delimiter=',', quoting=csv.QUOTE_NONE, escapechar="\\")

Example:

name\,item_det

Otherwise, don't use the csv module.

Sign up to request clarification or add additional context in comments.

Comments

1

Just make sure that any single values that contain your delimiter char (,) are enclosed in the quotechar (|, in your example). Thus, any time the name field in a row is of the form <name>, <item_det> the row should read something like ... ,|<name>, <item_det>|, ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.