Unicode to UTF8 for CSV Files - Python via xlrd?

I expect the cell_value return value is the unicode string that's giving you problems (please print its type() to confirm that), in which case you should be able to solve it by changing this one line: this_row. Append(s. Cell_value(row,col)) to: this_row.

Append(s. Cell_value(row,col). Encode('utf8')) If cell_value is returning multiple different types, then you need to encode if and only if it's returning a unicode string; so you'd split this line into a few lines: val = s.

Cell_value(row, col) if isinstance(val, unicode): val = val. Encode('utf8') this_row. Append(val).

Perfect! That did it. I suppose I didn't expect that the different value types would need to be treated differently.

Thanks! – anschauung Jul 27 '09 at 17:51.

You asked for explanations, but some of the phenomena are inexplicable without your help. (A) Strings in XLS files created by Excel 97 onwards are encoded in Latin1 if possible otherwise in UTF16LE. Each string carries a flag telling which was used.

Earlier Excels encoded strings according to the user's "codepage". In any case, xlrd produces unicode objects. The file encoding is of interest only when the XLS file has been created by 3rd party software which either omits the codepage or lies about it.

See the Unicode section up the front of the xlrd docs. (B) Unexplained phenomenon: This code: bcw = csv. Writer(bc,csv.

Excel,b. Encoding) causes the following error with Python 2.5, 2.6 and 3.1: TypeError: expected at most 2 arguments, got 3 -- this is about what I'd expect given the docs on csv. Writer; it's expecting a filelike object followed by either (1) nothing (2) a dialect or (3) one or more formatting parameters.

You gave it a dialect, and csv. Writer has no encoding argument, so splat. What version of Python are you using?

Or did you not copy/paste the script that you actually ran? (C) Unexplained phenomena around traceback and what the actual offending data was: "the_script. Py", line 40, in this_row.

Append(str(s. Cell_value(row,col))) UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 5: ordinal not in range(128) FIRSTLY, there's a str() in the offending code line that wasn't in the simplified script -- did you not copy/paste the script that you actually ran? In any case, you shouldn't use str in general -- you won't get the full precision on your floats; just let the csv module convert them.

SECONDLY, you say """The value is seems to be getting hung up on is "516-777316" -- the text in the original Excel sheet is "516-7773167" (with a 7 on the end)""" --- it's difficult to imagine how the 7 gets lost off the end. I'd use something like this to find out exactly what the problematic data was: try: str_value = str(s. Cell_value(row, col)) except: print "row=%d col=%d cell_value=%r" % (row, col, s.

Cell_value(row, col)) raise That %r saves you from typing cell_value=%s ... repr(s. Cell_value(row, col)) ... the repr() produces an unambiguous representation of your data. Learn it.

Use it. How did you arrive at "516-777316"? THIRDLY, the error message is actually complaining about a unicode character u'\xed' at offset 5 (i.e.

The sixth character). U+00ED is LATIN SMALL LETTER I WITH ACUTE, and there's nothing like that at all in "516-7773167" FOURTHLY, the error location seems to be a moving target -- you said in a comment on one of the solutions: "The error is on bcw.writerow. " Huh?(D) Why you got that error message (with str()): str(a_unicode_object) attempts to convert the unicode object to a str object and in the absence of any encoding information uses ascii, but you have non-ascii data, so splat.

Note that your object is to produce a csv file encoded in utf8, but your simplified script doesn't mention utf8 anywhere. (E) """... s. Cell(row,col)) (e.g.S.

Cell instead of s. Cell_value) the entire document writes without errors. The output isn't particularly desirable (text:u'516-7773167')""" That's happening because the csv writer is calling the __str__ method of your Cell object, and this produces : which may be useful for debugging but as you say not so great in your csv file.(F) Alex Martelli's solution is great in that it got you going.

However you should read the section on the Cell class in the xlrd docs: types of cell are text, number, boolean, date, error, blank and empty. If you have dates, you are going to want to format them as dates not numbers, so you can't use isinstance() (and you may not want the function call overhead anyway) ... this is what the Cell. Ctype attribute and Sheet.

Cell_type() and Sheet. Row_types() methods are for.(G) UTF8 is not Unicode. UTF16LE is not Unicode.

UTF16 is not Unicode ... and the idea that individual strings would waste 2 bytes each on a UTF16 BOM is too preposterous for even MS to contemplate :-) (H) Further reading (apart from the xlrd docs): http://www.joelonsoftware.com/articles/Unicode.html http://www.amk.ca/python/howto/unicode.

1: Thanks for the great explanation, and the background links. This has made me realize I can't avoid educating myself on encoding any longer, and I appreciate your going into such detail even after the immediate problem has been resolved. – anschauung Jul 28 '09 at 23:04.

Looks like you've got 2 problems. There's something screwed up in that cell - '7' should be encoded as u'x37' I think, since it's within the ASCII-range. More importantly though, the fact that you're getting an error message specifying that the ascii codec can't be used suggests something's wrong with your encoding into unicode - it thinks you're trying to encode a value 0xed that can't be represented in ASCII, but you said you're trying to represent it in unicode.

I'm not smart enough to work out what particular line is causing the problem - if you edit your question to tell me what line's causing that error message I might be able to help a bit more (I guess it's either this_row. Append(s. Cell_value(row,col)) or bcw.

Writerow(this_row), but would appreciate you confirming).

Thanks! The error is on bcw.writerow. Everything outputs correctly if, say, I use print this_row.

As best I can tell, there's nothing obviously wrong with the '7' -- it outputs correctly (as u'516-7773167') when I print to stdout. – anschauung Jul 27 '09 at 16:33 Then it looks like bcw. Writerow is expecting ASCII - are you sure you've got your arguments to csv.

Writer correct (see docs.python. Org/library/csv. Html#csv.

Writer)? I'm bemused as to where the 0xed is coming from. – Dominic Rodger Jul 27 '09 at 16:41.

There appear to be two possibilities. One is that you have not perhaps opened the output file correctly: "If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference. " ( docs.python.org/library/csv.html#module-csv ) If that is not the problem, then another option for you is to use codecs.

EncodedFile(file, input, output, errors) as a wrapper to output your . Csv: docs.python.org/library/codecs.html#modu... This will allow you to have the file object filter from incoming UTF16 to UTF8. While both of them are technically "unicode", the way they encode is very different.

Something like this: rbc = open('file. Csv','w') bc = codecs. EncodedFile(rbc, "UTF16", "UTF8") bcw = csv.

Writer(bc,csv. Excel) may resolve the problem for you, assuming I understood the problem right, and assuming that the error is thrown when writing to the file.

Well, it gave a different error message (this time before writing anything to the file): UnicodeDecodeError: 'utf16' codec can't decode byte 0x0a in position 938: truncated data – anschauung Jul 27 '09 at 16:38.

(G) UTF8 is not Unicode. UTF16LE is not Unicode.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions