help I'm getting delimited

aka alexoplocatie at gmail.com
Wed Dec 17 15:28:01 CET 2008


Hi John, thanks.
You're right, I didn't past the method header because I thought it
didn't matter when the input filename is hardcoded.
The try/except isn't very helpful indeed so I commented it out.
You're right I wrongly referred to the UnicodeReader
class in my first post because that's ultimately where I want to go
so
I outcommented it here for you to see.
The fact is that neither csv.reader nor the UnicodeReader will read
the file, while writing with the UnicodeWriter
works like a charm.
That's why I put str() around roles to see any content.
I simplified the csv-file by cutting off columns without result. The
file looks now like:

id;company;department
12;Cadillac;Research
11;Ford;Accounting
10;Chrysler;Sales


The dictionary on the return is because this code is part of my
TurboGears application.
The entire method is:


import csv
from utilities.urw       import UnicodeWriter, UnicodeReader


    @expose(allow_json=True)
    def import_roles(self, input=None, *args, **kwargs):
        inp = 'C:/temp/test.csv'
        roles = []
        msg = ''
        ## try:
        fp = open(inp, 'rb')
        reader = csv.reader(fp, dialect='excel', delimiter=';')
        ## reader = UnicodeReader(fp, dialect='excel', delimiter=';')
        for r in reader:
            roles.append(r[0])
        fp.close()
        ## except:
            ## msg = "Something's wrong with the csv.reader"
        return dict(filepath=inp,
                    roles=str(roles),
                    msg=msg)


csv.reader results in: for r in reader: Error: line contains NULL
byte


Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
can't decode byte 0xff in position 0: unexpected code byte


Will post only complete code from now on thanks.




More information about the Python-list mailing list