简体   繁体   English

python将“ unicode”转换为列表

[英]python convert “unicode” as list

I have a doubt about treat a return type in python. 我对在python中处理返回类型有疑问。

I have a database function that returns this as value: 我有一个数据库函数,将其作为值返回:

(1,13616,,"My string, that can have comma",170.90)

I put this into a variable and did test the type: 我将其放入变量并测试了类型:

print(type(var))

I got the result: 我得到了结果:

<type 'unicode'>

I want to convert this to a list and get the values separeteds by comma. 我想将其转换为列表,并用逗号分隔值。

Ex.: 例如:

var[0] = 1
var[1] = 13616
var[2] = None
var[3] = "My string, that can have comma"
var[4] = 170.90

Is it possible? 可能吗?

Using standard library csv readers: 使用标准库csv阅读器:

>>> import csv
>>> s = u'(1,13616,,"My string, that can have comma",170.90)'
>>> [var] = csv.reader([s[1:-1]])
>>> var[3]
'My string, that can have comma'

Some caveats: 一些警告:

  • var[2] will be an empty string, not None , but you can post-process that. var[2]将是一个空字符串,而不是None ,但是您可以对其进行后处理。
  • numbers will be strings and also need post-processing, since csv does not tell the difference between 0 and '0' . 数字将是字符串,并且也需要后处理,因为csv不能告诉0'0'之间的差异。

You can try to do the following: 您可以尝试执行以下操作:

b = []
for i in a:
    if i != None:
        b.append(i)
    if i == None:
        b.append(None)

print (type(b))

The issue is not with the comma. 问题不在于逗号。

this works fine: 这工作正常:

a = (1,13616,"My string, that can have comma",170.90)

and this also works: 这也适用:

a = (1,13616,None,"My string, that can have comma",170.90)

but when you leave two commas ",," it doesn't work. 但是当您留下两个逗号“,”时,它不起作用。

Unicode strings are (basically) just strings in Python2 (in Python3, remove the word "basically" in that last sentence). Unicode字符串(基本上)只是Python2中的字符串(在Python3中,删除最后一句话中的“基本上”一词)。 They're written as literals by prefixing a u before the string (compare raw-strings r"something" , or Py3.4+ formatter strings f"{some_var}thing" ) 通过在字符串前面加上u来将它们写为文字(比较原始字符串r"something"或Py3.4 +格式化程序字符串f"{some_var}thing"

Just strip off your parens and split by comma. 只需剥离您的括号并以逗号分隔即可。 You'll have to do some post-parsing if you want 170.90 instead of u'170.90' or None instead of u'' , but I'll leave that for you to decide. 如果要170.90而不是u'170.90'None而不是u'' ,则必须进行一些后期解析,但是我留给您自己决定。

>>> var.strip(u'()').split(u',')
[u'1', u'13616', u'', u'"My string', u' that can have comma"', u'170.90']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM