[英]How to remove square brackets from string?
我想删除代码输出中的两个方括号。
我的代码:
request2 = requests.get('https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId=1045618&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
json2 = request2.json()
for selection in json2['selections']:
for fluc in selection['flucs'][0]:
flucs1 = ast.literal_eval(selection['flucs'])
flucs2 = flucs1[-2:]
flucs3 = [[x[1]] for x in flucs2]
代码输出示例:
[[12.97], [13.13]]
所需的代码输出:
12.97, 13.13
使用 str.replace 方法
n = [[12.97], [13.13]]
m = str(n)[1:-1] # convert list into str to be able to use str.replace method
z = m.replace('[', '', 3)
y = z.replace(']', '', 3)
print(y)
输出
12.97, 13.13
或使用正则表达式
import re
al = [1, 2, [5, 6], 8, 9]
z = re.sub(r'\[', '', str(al))
y = re.sub(r'\]','', z)
print(y)
输出
1, 2, 5, 6, 8, 9
.join()
也有助于像这样加入列表列表:
output = [[12.97], [13.13]]
result = '\n'.join(','.join(map(str, row)) for row in output)
print(result)
输出 :
12.97
13.13
也试试这个:
result2 = ', '.join(','.join(map(str, row)) for row in output)
print(result2)
输出:
12.97, 13.13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.