help I'm getting delimited

John Machin sjmachin at lexicon.net
Wed Dec 17 10:23:40 EST 2008


On Dec 18, 1:28 am, aka <alexoploca... at gmail.com> 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
> 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

Pardon my ignorance, but what is "utilities.urw"??

>
>     @expose(allow_json=True)

Means what? Does what?
Does the problem still happen without that?

Funny, the indentation changed there --- for the very last time, is
that the actual code of a standalone script that reproduces the
problem?


>     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

Looks like the file is stuffed. Have you tried inspecting it with a
tool that would actually show a '\x00' or a '\xff' unambiguously? If
you don't have a fancy one, use the Python interactive prompt:

   open('your_file.csv', 'rb').read()
>
> 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.

Just make sure it's runnable and it's what you actually ran thanks.



More information about the Python-list mailing list