Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
69819 Lượt xem

Hi,

Please i need help to correct this error. 

the code line whose raise error is : 

status = (str(each.get('status'))).lower()

I am using odoo 10.

thanks for you help.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You need an *Unicode* codec to encode your data.

    each.get('status')

gives you an unicode string (bytes type in Python 2), and then str(...) will use ascii codec to encode it with hope that will give you a string (str type in Python 2). But it failed because it could not handle u'\xe9'. What you need to get pass this is some thing similar to this:

     each.get('status').encode('utf-8')

this technically works and result will be a  str and you can call .lower() on it. But please check the final result, if you give it an upper É, i'm not sure that it will give you the lower é.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

On Windows, many editors assume the default ANSI encoding (CP1252 on US Windows) instead of UTF-8 if there is no byte order mark (BOM) character at the start of the file. Files store bytes, which means all unicode have to be encoded into bytes before they can be stored in a file. read_csv takes an encoding option to deal with files in different formats. So, you have to specify an encoding, such as utf-8.

 df.to_csv('D:\panda.csv',sep='\t',encoding='utf-8')

If you don't specify an encoding, then the encoding used by df.tocsv defaults to ascii in Python2, or utf-8 in Python3.

Also, you can encode a problematic series first then decode it back to utf-8.

df['column-name'] = df['column-name'].map(lambda x: x.encode('unicode-escape').decode('utf-8'))

This will also rectify the problem.

http://net-informations.com/ds/pd/tocsv.htm

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 3 15
8376
1
thg 12 24
3886
0
thg 11 24
1600
2
thg 7 24
3272
0
thg 4 24
2122