[英]remove inverted commas from python string
I have a very annoying string formatting issue.我有一个非常烦人的字符串格式问题。 I tried different approaches but i seem to be lost.我尝试了不同的方法,但我似乎迷路了。 This is my expected output: ["aa","b","c","dd"].这是我预期的 output: ["aa","b","c","dd"]。
Sample code:示例代码:
mylist = ['b','c']
mylisttmp = ','.join('"{0}"'.format(x) for x in mylist)
finalstr='"aa"' +","+"{}".format(mylisttmp) +","+'"dd"'
print([finalstr])
OUTPUT:['"aa","b","c","dd"'] #How to get rid of the end quotes,which is causing issues?
I did a lot of string splitting, joining etc but I am going round and round the same issue.我做了很多字符串拆分,加入等,但我正在围绕同一个问题。 I intended to use the formatted output with a tkinter property, as follows:我打算使用带有 tkinter 属性的格式化 output ,如下所示:
myComboBox['values']= ["aa","b","c","dd"]
Please direct me.请指导我。 Thank you谢谢
I mean you already are appending aa
and dd
, then why not just add open and close square brackets with them?我的意思是您已经在附加aa
和dd
,那么为什么不直接添加开方括号和右方括号呢?
mylist = ['b','c']
mylisttmp = ','.join('"{0}"'.format(x) for x in mylist)
finalstr='["aa"' +","+"{}".format(mylisttmp) +","+'"dd"]'
print(finalstr)
Output Output
["aa","b","c","dd"]
This is essentially two fundamental misunderstandings of what you're seeing.这本质上是对您所看到的内容的两个基本误解。
and和
["a","b"]
, but can probably only ever get ['a','b']
, you see that as an error, but in fact they're the same.因此,当您想要["a","b"]
,但可能只能得到['a','b']
时,您会将其视为错误,但实际上它们是相同的。 So in your case, what you probably want is something like ['aa','b','c','dd']
.因此,就您而言,您可能想要的是['aa','b','c','dd']
类的东西。 Don't mess around with the string formatting at all.根本不要乱用字符串格式。
You don't actually want a string at all.你实际上根本不想要一个字符串。 You want a list that contains four strings.您需要一个包含四个字符串的列表。
mylist = ['b','c']
finalset = ["aa"] + mylist + ["dd"]
print(finalset)
Output Output
>>> print(finalset)
['aa', 'b', 'c', 'dd']
>>>
print(f'["aa",{mylisttmp},"dd"]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.