解码元组中的unicode字符串

时间:2022-10-17 08:32:31

I have the following set of simple examples of code, none of which are doing what I want:

我有以下一组简单的代码示例,其中没有一个是我想要的:

import json

a = "u'Kolo Tour\xe9'"
print a

b = (a.decode('cp1252'), 1)
print b

c = (a, 1)
print c

d = ','.join((b.decode('cp1252')))
print d

The final example is throwing up an error about a tuple having no method for decoding. What I want my final item to look like is:

最后一个例子是抛出一个没有解码方法的元组错误。我希望我的最终项目看起来像是:

Kolo Touré,1

Can anyone tell me what I am doing wrong and what the correct syntax I need to fix my issue is please?

任何人都可以告诉我我做错了什么以及我需要解决我的问题的正确语法是什么?

Thanks

EDIT:

A segment of the actual source data I am attempting to parse in my full scale code prints like this to the screen in both command shell and python IDLE:

我尝试在我的完整规模代码中解析的实际源数据的一部分打印到命令shell和python IDLE中的屏幕:

(u'Jos\xe9 Enrique', 14230, 29, 3, u'DL', 184, 76, True, False)

1 个解决方案

#1


1  

Printing a tuple displays the representation of the contents, so it is extremely unlikely that you have a unicode literal in a string.

打印元组会显示内容的表示形式,因此在字符串中使用unicode文字极不可能。

newdata = (olddata[0], 1)

EDIT:

Based on your latest output:

根据您的最新输出:

u'{},1'.format(olddata[0])

but I'm wondering if you don't want to use csv instead for output generation.

但我想知道你是否不想使用csv来代替输出。

#1


1  

Printing a tuple displays the representation of the contents, so it is extremely unlikely that you have a unicode literal in a string.

打印元组会显示内容的表示形式,因此在字符串中使用unicode文字极不可能。

newdata = (olddata[0], 1)

EDIT:

Based on your latest output:

根据您的最新输出:

u'{},1'.format(olddata[0])

but I'm wondering if you don't want to use csv instead for output generation.

但我想知道你是否不想使用csv来代替输出。