使用Urllib时,如何使用CSV文件中的第二列命名图像文件?

发布于 2025-02-02 16:48:53 字数 492 浏览 3 评论 0原文

我有要在CSV文件中下载的100张图像的列表。 CSV上没有标题。第一列具有图像的位置,第二列的名称我希望将图像保存为。

我正在使用urllib下载图像。我想为循环设置,该循环使用第一列中的位置,并从第二列中的数据命名。我在这里放了的行是错误的,但是希望您能得到这个想法 - urllib.request.urlretrieve('url [0]','url [1]') - 我正在尝试拉动从第一列或第二列。

urls = []
f = open('multicoltest.csv', encoding='utf-8-sig')
csv_f = csv.reader(f)

for row in csv_f:
    urls.append(row[0])
f.close()

for url in urls:
    urllib.request.urlretrieve('url[0]', 'url[1]')

I have a list of 100 images I want to download in a CSV file. There are no headers on the CSV. The first column has the location of the image and the second column has the name I would like the image saved as.

I am using urllib to download the images. I would like to setup a for loop which uses the location from the first column and names it from the data in the second column. The line I put in here is wrong, but hopefully you get the idea - urllib.request.urlretrieve('url[0]', 'url[1]') - I'm trying to pull from the first or second column.

urls = []
f = open('multicoltest.csv', encoding='utf-8-sig')
csv_f = csv.reader(f)

for row in csv_f:
    urls.append(row[0])
f.close()

for url in urls:
    urllib.request.urlretrieve('url[0]', 'url[1]')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2025-02-09 16:48:53

您可以一次执行一行,而无需首先阅读整个文件:

with open('multicoltest.csv', encoding='utf-8-sig') as f:
    for row in csv.reader(f):
        urllib.request.urlretrieve(row[0], row[1])

You can do it a line at a time without reading the entire file first:

with open('multicoltest.csv', encoding='utf-8-sig') as f:
    for row in csv.reader(f):
        urllib.request.urlretrieve(row[0], row[1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文