help I'm getting delimited
J. Cliff Dyer
jcd at sdf.lonestar.org
Thu Dec 18 16:03:44 CET 2008
On Wed, 2008-12-17 at 06:28 -0800, aka wrote:
> 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
> 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:
> 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
> 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:
> ## except:
> ## msg = "Something's wrong with the csv.reader"
> return dict(filepath=inp,
> csv.reader results in: for r in reader: Error: line contains NULL
> Use of UnicodeReader results in: UnicodeDecodeError: 'utf8' codec
> can't decode byte 0xff in position 0: unexpected code byte
This looks like the problem might be in your choice of codec. A UTF-8
file will never have 0xff in it, and would be unlikely to have 0x00
either. My guess is that you will need to decode your input from
UTF-16. (and then use the UnicodeReader).
> Will post only complete code from now on thanks.
More information about the Python-list