如何在Python中将列表值附加到URL
我有一个基本url,我想将列表值附加到其中,这样我就有了一个url列表。我的列表来自一个json文件,如下所示:如何在Python中将列表值附加到URL,python,Python,我有一个基本url,我想将列表值附加到其中,这样我就有了一个url列表。我的列表来自一个json文件,如下所示: [{u'url': [u'/location/subfile/file1.htm', u'/location/subfile/file2.htm', u'/location/subfile/file3.htm', u'/location/subfile/file4.htm']}] 我的基本URL类似于http://example.com/placeforfiles/ 最终,我想要的
[{u'url': [u'/location/subfile/file1.htm', u'/location/subfile/file2.htm', u'/location/subfile/file3.htm', u'/location/subfile/file4.htm']}]
我的基本URL类似于http://example.com/placeforfiles/
最终,我想要的是一组URL,这些URL包含基本URL和列表值,如下所示:
http://example.com/placeforfiles/location/subfile/file1.htm
http://example.com/placeforfiles/location/subfile/file2.htm
http://example.com/placeforfiles/location/subfile/file3.htm
http://example.com/placeforfiles/location/subfile/file4.htm
我可能需要附加数千个列表值,所以我知道我需要循环并附加它们,但我还没有找到一个有效的解决方案。我目前正在尝试:
import json
with open ('returned_items.json') as links:
data = json.load(links)
base_url = 'http://example.com/placeforfiles/{}'
for i in data:
url = 'http://example.com/placeforfiles/{}'.format(i)
print url
它正在返回:
http://example.com/placeforfiles/({u'url': [u'/location/subfile/file1.htm', u'/location/subfile/file2.htm', u'/location/subfile/file3.htm', u'/location/subfile/file4.htm']},)
这是因为dict是数组中的第一个元素。
对于数据[0][“url”]中的i,循环应该是
,这是因为dict是数组中的第一个元素。
对于数据[0][“url”]
中的i,循环应该是。抱歉,刚刚意识到问题在那里是出于意外,我编辑了问题以将其删除。抱歉,刚刚意识到问题在那里是出于意外,我编辑了问题以将其删除。
#replcace data with below line
data = json.loads(links)
#replace your last loop with below
if data and 'url' in data[0]:
for i in data[0]['url']:
url = 'http://example.com/placeforfiles{}'.format(i)
print(url)