python - 如何从字符串中删除方括号?
问题描述
我想删除代码输出中的两个方括号。
我的代码:
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
解决方案
.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
推荐阅读
- angularjs - 为什么一个未渲染的元素表现得好像它已经被渲染了?
- javascript - Service Worker 同步加载
- java - Spring 批量读取 REST api 并使用 JdbcBatchItemWriter 将一次读取的多条记录写入单个 DB 表的代码
- c - C 程序在使用 Scanf 时崩溃,在 int 输入后
- sql - Postgresql 加入数组并转换为 json
- html - 当我减小窗口大小而不是设置为移动视口时,文档会自动缩放
- delphi - Datasnap 客户端回调连接 KeepAlive 不起作用
- homebrew - 如何在 OSX 上安装 apg(密码生成器)?
- c - 无法释放 avltree 中的根
- protractor - 试图将 e2e 代码移动到函数中但没有成功