[英]python list to format and replace data
從網站請求數據將返回一個看起來像這樣的列表。
[
"https://site1.com/:hash",
"https://site2.com/:hash",
"https://site3.com/:hash",
"https://site4.com/:hash",
"https://site5.com/:hash"
]
我試圖遍歷該列表,並用一個等於cats的變量替換:hash 。 從列表的額外格式,加上額外的標點符號和搜索/替換,我感到很困惑。 任何其他幫助將不勝感激。
https://site1.com/cats
https://site2.com/cats
https://site3.com/cats
https://site4.com/cats
https://site5.com/cats
#!/usr/bin/env python3
import os
import requests
gw_path = 'https://raw.githubusercontent.com/ipfs/public-gateway-checker/master/gateways.json'
r = requests.get(gw_path)
text = r.text
for item in text:
mod = item.replace(':hash', 'cats')
print(mod)
。
如果a
是初始列表,請使用列表推導 :
a = ["https://site1.com/:hash",
"https://site2.com/:hash",
"https://site3.com/:hash",
"https://site4.com/:hash",
"https://site5.com/:hash"]
modified_a = [i.replace(':hash', 'cats') for i in a]
modified_a
['https://site1.com/cats',
'https://site2.com/cats',
'https://site3.com/cats',
'https://site4.com/cats',
'https://site5.com/cats']
和:
print "[%s]" % (','.join(modified_a)
[https://site1.com/cats,https://site2.com/cats,https://site3.com/cats,https://site4.com/cats,https://site5.com/cats]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.