Python3 url从文件和列表连接
我有一个文件,有多个url和url路径列表。我试图将它们与列表中的url路径连接在一起。我在试着让它工作时遇到了很多麻烦 文件有如下URLPython3 url从文件和列表连接,python,python-3.x,list,concatenation,Python,Python 3.x,List,Concatenation,我有一个文件,有多个url和url路径列表。我试图将它们与列表中的url路径连接在一起。我在试着让它工作时遇到了很多麻烦 文件有如下URL foobar.com foobar.com.tk foobar.org 我不知道这是否重要,但带有URL的文件没有http://头,当我尝试将列表中的URL与路径连接时,我不断收到错误,或者代码都聚在一起……如何将文件中的URL与列表中的路径连接起来 您可以通过itertools.product获得每个url组合: from itertools impor
foobar.com
foobar.com.tk
foobar.org
我不知道这是否重要,但带有URL的文件没有http://头,当我尝试将列表中的URL与路径连接时,我不断收到错误,或者代码都聚在一起……如何将文件中的URL与列表中的路径连接起来 您可以通过
itertools.product
获得每个url组合:
from itertools import product
from pprint import pprint
list1 = ["/foobar.php", "/foobar.html", "/foobar.php"]
with open("file1.txt") as f:
pprint(
set(
"https://%s%s" % (root, path)
for root, path in product(map(str.strip, f), list1)
)
)
网址:
注意:您可以在此处使用set()
从结果中删除重复的URL
list1 = ['/foobar.php','/foobar.html','/foobar.php']
with open('file1.txt') as f:
Nlist = [line.strip() for line in f]
for i in range(len(Nlist)):
pth = 'https://' + Nlist[i] + list1[i]
print(pth)
os.path.join()
似乎不喜欢所有的dot whatever,因此您似乎必须求助于自己的连接。您能否共享此示例数据的预期输出?这将有助于澄清问题。请提供有关错误和任何实际/预期输出的更多详细信息。您可以使用list\u a+list\u b
连接列表,而不管它们的数据来自何处。哦,是的,很抱歉我本打算这么做,但是忘记了输出应该是这样的```或foobar.com/foobar.php foobar.org/foobar.html foobar.com.tk/foobar.php``那么我必须先将文件放入列表吗?我正在尝试遍历它们,这样我就可以对每个单独的url发出post请求…如果我列出a+列表b,它只是将列表添加到一起,我需要能够将文件中的url与列表中的路径连接起来,这样它们就可以形成这样的url…lines=list(open(filename,'r'))
{'https://foobar.com.tk/foobar.html',
'https://foobar.com.tk/foobar.php',
'https://foobar.com/foobar.html',
'https://foobar.com/foobar.php',
'https://foobar.org/foobar.html',
'https://foobar.org/foobar.php'}
list1 = ['/foobar.php','/foobar.html','/foobar.php']
with open('file1.txt') as f:
Nlist = [line.strip() for line in f]
for i in range(len(Nlist)):
pth = 'https://' + Nlist[i] + list1[i]
print(pth)