简体   繁体   English

从列表的第一个元素中删除双引号

[英]remove double quotes from first element of the list

I want to remove double quotes from this list: 我想从此列表中删除双引号:

>>> a
["6/1, 0, 0, ['79', '80', '81', '82']"]

I tried converting first element of list to list which is a string but dint work: 我尝试将list的第一个元素转换为list,但它是一个字符串,但比较简单:

>>> a[0]
"6/1, 0, 0, ['79', '80', '81', '82']"

>>> list(a[0])
['6', '/', '1', ',', ' ', '0', ',', ' ', '0', ',', ' ', '[', "'", '7', '9', "'", ',', ' ', "'", '8', '0', "'", ',', ' ', "'", '8', '1', "'", ',', ' ', "'", '8', '2', "'", ']']

Expected output: 预期产量:

>>> a
[6/1, 0, 0, ['79', '80', '81', '82']]
                    or
>>> a
['6/1', '0', '0', ['79', '80', '81', '82']]

Can someone please help me resolving this issue? 有人可以帮我解决这个问题吗?

This will evaluate the string as a list: 这会将字符串评估为列表:

a = list(eval(a[0]))

[6, 0, 0, ['79', '80', '81', '82']]

If you know your port will be 6/1, you can replace it with a string like this: 如果您知道端口将是6/1,则可以将其替换为以下字符串:

a = list(eval(a[0]))
a[0] = '6/1'

['6/1', 0, 0, ['79', '80', '81', '82']]

If your port is going to change, but you know it will always be the first element, you can do this: 如果您的端口将要更改,但是您知道它将始终是第一个元素,则可以执行以下操作:

a = list(eval("'" + a[0].split(',')[0] + "'" + "," + ','.join(a[0].split(',')[1:])))

['6/1', 0, 0, ['79', '80', '81', '82']]

Or cleaner version: 或更清洁的版本:

l = a[0].split(',')
a = list(eval("'" + l[0] + "' ," + ','.join(l[1:])))

['6/1', 0, 0, ['79', '80', '81', '82']]

x= ["6/1, 0, 0, ['79', '80', '81', '82']" ]
a=x[0].replace('\'','').split(', [')
finallist=a[0].split(',')
finallist.append(a[1].replace(']','').split(','))
print (finallist)

output 输出

['6/1', ' 0', ' 0', ['79', ' 80', ' 81', ' 82']]

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

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