I have a string returnd from a software like "('mono')"
from that I needed to convert string to tuple .
我有一个像“('mono')这样的软件的字符串返回”,我需要将字符串转换为元组。
that I was thinking using ast.literal_eval("('mono')")
but it is saying malformed string.
我正在考虑使用ast.literal_eval(“('mono')”),但它说的是格式错误的字符串。
5 个解决方案
#1
Since you want tuples, you must expect lists of more than element in some cases. Unfortunately you don't give examples beyond the trivial (mono)
, so we have to guess. Here's my guess:
既然你想要元组,那么在某些情况下你必须要有超过元素的列表。不幸的是,除了琐碎的(单声道)之外你不会给出例子,所以我们不得不猜测。这是我的猜测:
"(mono)"
"(two,elements)"
"(even,more,elements)"
If all your data looks like this, turn it into a list by splitting the string (minus the surrounding parens), then call the tuple constructor. Works even in the single-element case:
如果您的所有数据都是这样,请通过拆分字符串(减去周围的parens)将其转换为列表,然后调用元组构造函数。甚至在单元素情况下也可以工作:
assert data[0] == "(" and data[-1] == ")"
elements = data[1:-1].split(",")
mytuple = tuple(elements)
Or in one step: elements = tuple(data[1:-1].split(","))
. If your data doesn't look like my examples, edit your question to provide more details.
或者一步:elements = tuple(data [1:-1] .split(“,”))。如果您的数据与我的示例不同,请修改您的问题以提供更多详细信息。
#2
How about using regular expressions ?
如何使用正则表达式?
In [1686]: x
Out[1686]: '(mono)'
In [1687]: tuple(re.findall(r'[\w]+', x))
Out[1687]: ('mono',)
In [1688]: x = '(mono), (tono), (us)'
In [1689]: tuple(re.findall(r'[\w]+', x))
Out[1689]: ('mono', 'tono', 'us')
In [1690]: x = '(mono, tonous)'
In [1691]: tuple(re.findall(r'[\w]+', x))
Out[1691]: ('mono', 'tonous')
#3
Try to this
试试这个
a = ('mono')
print tuple(a) # <-- you create a tuple from a sequence
#(which is a string)
print tuple([a]) # <-- you create a tuple from a sequence
#(which is a list containing a string)
print tuple(list(a))# <-- you create a tuple from a sequence
# (which you create from a string)
print (a,)# <-- you create a tuple containing the string
print (a)
Output :
('m', 'o', 'n', 'o')
('mono',)
('m', 'o', 'n', 'o')
('mono',)
mono
#4
I assume that the desired output is a tuple with a single string: ('mono',)
我假设所需的输出是一个带有单个字符串的元组:('mono',)
A tuple of one has a trailing comma in the form (tup,)
一个元组的元组在表单中有一个尾随逗号(tup,)
a = '(mono)'
a = a[1:-1] # 'mono': note that the parenthesis are removed removed
# if they are inside the quotes they are treated as part of the string!
b = tuple([a])
b
> ('mono',)
# the final line converts the string to a list of length one, and then the list to a tuple
#5
Convert string to tuple? Just apply tuple
:
将字符串转换为元组?只需应用元组:
>>> tuple('(mono)')
('(', 'm', 'o', 'n', 'o', ')')
Now it's a tuple.
现在它是一个元组。
#1
Since you want tuples, you must expect lists of more than element in some cases. Unfortunately you don't give examples beyond the trivial (mono)
, so we have to guess. Here's my guess:
既然你想要元组,那么在某些情况下你必须要有超过元素的列表。不幸的是,除了琐碎的(单声道)之外你不会给出例子,所以我们不得不猜测。这是我的猜测:
"(mono)"
"(two,elements)"
"(even,more,elements)"
If all your data looks like this, turn it into a list by splitting the string (minus the surrounding parens), then call the tuple constructor. Works even in the single-element case:
如果您的所有数据都是这样,请通过拆分字符串(减去周围的parens)将其转换为列表,然后调用元组构造函数。甚至在单元素情况下也可以工作:
assert data[0] == "(" and data[-1] == ")"
elements = data[1:-1].split(",")
mytuple = tuple(elements)
Or in one step: elements = tuple(data[1:-1].split(","))
. If your data doesn't look like my examples, edit your question to provide more details.
或者一步:elements = tuple(data [1:-1] .split(“,”))。如果您的数据与我的示例不同,请修改您的问题以提供更多详细信息。
#2
How about using regular expressions ?
如何使用正则表达式?
In [1686]: x
Out[1686]: '(mono)'
In [1687]: tuple(re.findall(r'[\w]+', x))
Out[1687]: ('mono',)
In [1688]: x = '(mono), (tono), (us)'
In [1689]: tuple(re.findall(r'[\w]+', x))
Out[1689]: ('mono', 'tono', 'us')
In [1690]: x = '(mono, tonous)'
In [1691]: tuple(re.findall(r'[\w]+', x))
Out[1691]: ('mono', 'tonous')
#3
Try to this
试试这个
a = ('mono')
print tuple(a) # <-- you create a tuple from a sequence
#(which is a string)
print tuple([a]) # <-- you create a tuple from a sequence
#(which is a list containing a string)
print tuple(list(a))# <-- you create a tuple from a sequence
# (which you create from a string)
print (a,)# <-- you create a tuple containing the string
print (a)
Output :
('m', 'o', 'n', 'o')
('mono',)
('m', 'o', 'n', 'o')
('mono',)
mono
#4
I assume that the desired output is a tuple with a single string: ('mono',)
我假设所需的输出是一个带有单个字符串的元组:('mono',)
A tuple of one has a trailing comma in the form (tup,)
一个元组的元组在表单中有一个尾随逗号(tup,)
a = '(mono)'
a = a[1:-1] # 'mono': note that the parenthesis are removed removed
# if they are inside the quotes they are treated as part of the string!
b = tuple([a])
b
> ('mono',)
# the final line converts the string to a list of length one, and then the list to a tuple
#5
Convert string to tuple? Just apply tuple
:
将字符串转换为元组?只需应用元组:
>>> tuple('(mono)')
('(', 'm', 'o', 'n', 'o', ')')
Now it's a tuple.
现在它是一个元组。