Python 如何将http附加到每个url的文本文件中

Python 如何将http附加到每个url的文本文件中,python,Python,我有一个没有http头的URL的大列表。 我正在努力完成两件事: 读取没有HTTP头exp www.google.com的URL文本文件,并将其拆分为1000个文本文件块 将http://附加到每个链接exp 目前我只能完成第一步 from itertools import zip_longest def grouper(n, iterable, fillvalue=None): "Collect data into fixed-length chunks or blocks"

我有一个没有http头的URL的大列表。 我正在努力完成两件事:

读取没有HTTP头exp www.google.com的URL文本文件,并将其拆分为1000个文本文件块

将http://附加到每个链接exp

目前我只能完成第一步

from itertools import zip_longest

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return zip_longest(fillvalue= fillvalue, *args)

n = 1000

with open('sites.txt') as f:
    for i, g in enumerate(grouper(n, f, fillvalue=''), 1):
        with open('s_{0}'.format(i), 'w') as fout:
            fout.writelines(g)

假设我正确理解了这个问题,因为它非常不清楚。。。您可以将字符串附加到列表中的每个项目,这非常简单:

def addtoeachitem(word, list):
    return [word+item for item in list]
这和写作是一样的

def addtoeachitem(word, list):
    new = []
    for item in list:
        new.append(word+item)
    return new
显然,这段代码假设列表中的所有内容都是字符串,如果不是,它将出错。根据自己的需要进行调整

将http://附加到每个链接exp

如果您有一个URL列表,并且希望在每个项目前面加上https://前缀,那么可以使用列表理解和字符串格式

urls = ['https://{}'.format(url) for url in urls]
如果文件中包含这些内容,请在换行符上拆分文件以创建列表:

with open('sites.txt') as f:
    urls = ['https://{}'.format(url) for url in f.splitlines()]

**注意:您的问题与HTTP标题无关

这应该只是HTTP://+g,否?您是否尝试添加HTTP://文本?我在您的代码中没有看到任何证据。您在这里显示的代码将生成一个缩进错误,实际上至少会生成两个缩进错误。请回答您的问题并修复代码的缩进。我们无法知道哪些错误是相关的,哪些不是。