Python list to make double quotes around string and pass to API, which requires this to pass as list of double quote string,
Python列表围绕字符串制作双引号并传递给API,这需要将其作为双引号字符串列表传递,
API data to pass:
要传递的API数据:
data = {
"styles" : styleList
}
it is working when I put manually:
当我手动放置时它正在工作:
["A123", "B123", "C131", "D231"]
But not with:
但不是:
['A123', 'B123', 'C131', 'D231']
Things tried and not helpful:
事情尝试过但没有帮助:
- Append double quotes around string.
在字符串周围附加双引号。
styleList = ["\"" + style + "\"" for style in styleList]
- Replace single quotes with double quotes.
用双引号替换单引号。
styleList = [style.replace("'","") for style in styleList]
- Dump as JSON.
转储为JSON。
styleList = json.dumps(styleList)
All are only helpful to print not to pass with API.
所有这些只对打印不通过API有帮助。
1 个解决方案
#1
0
You need a string representation of your list.
您需要列表的字符串表示。
With styleList = ["\"" + style + "\"" for style in styleList]
you create " around the items of your list: ['a','b'] --> ['"a"','"b"']
使用styleList = [“\”“+ style +”\“”for styleList中的样式],您可以创建“围绕列表中的项目:['a','b'] - > ['”a“',' “b”']
Use
data = { "styles" : repr(styleList) }`
repr
will create a string representation of your list: repr()
repr将创建列表的字符串表示形式:repr()
data = [1,2,3,'tata',8.9]
r = repr(data) # "[1, 2, 3, 'tata', 8.9]"
If you need numbers quoted as well, use
如果您还需要引用数字,请使用
# convert anything to its string representation
r = repr( [str(e) for e in data] ) # "['1', '2', '3', 'tata', '8.9']"
#1
0
You need a string representation of your list.
您需要列表的字符串表示。
With styleList = ["\"" + style + "\"" for style in styleList]
you create " around the items of your list: ['a','b'] --> ['"a"','"b"']
使用styleList = [“\”“+ style +”\“”for styleList中的样式],您可以创建“围绕列表中的项目:['a','b'] - > ['”a“',' “b”']
Use
data = { "styles" : repr(styleList) }`
repr
will create a string representation of your list: repr()
repr将创建列表的字符串表示形式:repr()
data = [1,2,3,'tata',8.9]
r = repr(data) # "[1, 2, 3, 'tata', 8.9]"
If you need numbers quoted as well, use
如果您还需要引用数字,请使用
# convert anything to its string representation
r = repr( [str(e) for e in data] ) # "['1', '2', '3', 'tata', '8.9']"