简体   繁体   English

如何使用 url 从 csv 文件下载/保存图像到我本地 Windows 机器上的特定创建文件夹中?

[英]How to download/save images from a csv file using url into specific created folder on my local Windows Machine?

I am trying to download the images which are available as url in csv file into specific folders (named as Male, Female for classification) I created in my C:/Desktop/Images.我正在尝试将 csv 文件中的 url 可用的图像下载到我在 CZtop/Images 中创建的特定文件夹中(命名为男性,女性用于分类)。 But when I run the below code, nothing is getting downloaded and getting saved into the specific folders based on the category as present in my csv file.但是当我运行下面的代码时,没有任何内容被下载并保存到基于我的 csv 文件中存在的类别的特定文件夹中。 The contents in my csv are as in below format.我的 csv 中的内容如下所示。 It has several thousands of rows.它有几千行。 I am trying to iterate and save the specific gender image to that particular folder created above我正在尝试迭代并将特定性别图像保存到上面创建的特定文件夹中

Format:
        Male   profilename    https://pbs.twimg.com/profile_images/414342229096808449/fYvzqXN7_normal.png

**Code:**
import urllib.request
import urllib.error
import sys

filename = "images"

with open("{0}.csv".format(filename), 'r') as csvfile:
    i = 0
    for line in csvfile:
        splitted_line = line.split(',')
        try:
            if ((splitted_line[2] != "\n") and (splitted_line[2] != '') and (splitted_line[0] == male)):
                fullfilename_1 = os.path.join('C:/Desktop/Images/Male', splitted_line[1])
                urllib.request.urlretrieve(splitted_line[2], fullfilename_1 + ".png")
                print ("Image saved for {0} in {1} ".format(splitted_line[1],'C:/Desktop/Images/Male'))
                i += 1
        except:
                print ("No result for {0}".format(splitted_line[1]))
        try:
            if ((splitted_line[2] != "\n") and (splitted_line[2] != '') and (splitted_line[0] == female)):
                fullfilename_1 = os.path.join('C:/Desktop/Images/Female', splitted_line[1])
                urllib.request.urlretrieve(splitted_line[2], fullfilename_1 + ".png")
                print ("Image saved for {0} in {1} ".format(splitted_line[1],'C:/Desktop/Images/Female'))
                i += 1
        except:
                print ("No result for {0}".format(splitted_line[1]))

How would I be able to download/save to the specific folder as mentioned?我如何能够下载/保存到提到的特定文件夹? Is there any issue with my path not being properly mentioned?我的路径没有被正确提及有什么问题吗? Any help would kindly be appreciated!任何帮助将不胜感激!

First change "images.csv" to "images.txt", then run this code:首先将“images.csv”更改为“images.txt”,然后运行以下代码:

import urllib.request
data = open("images.txt")
lines = data.readlines()

for row in lines:
    res = row.split(",")
    if len(res) < 3 or len(res) > 3:
        print ("Improper row.")
        continue
    if res[1].endswith(".jpg") == False and res[1].endswith(".png") == False:
        print ("Image not found.")
        continue
    try:
        location = f"C:/Desktop/Images/{res[0]}/"
        urllib.request.urlretrieve(res[2], location + res[1].split("/")[-1])
        print (f"Saved {res[1].split('/')[-1]} to {location[:-1]}.")
    except:
        print ("An error occured.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将pysftp中的csv文件下载到本地计算机? 无法将其保存在本地路径上,或将其加载到df中 - How to download csv file from pysftp to local machine? Can't get it to save on local path, or load it to a df 从 CSV 中的 Urls 下载图像到本地文件夹 Python 中的文件 - Download Images to Local Folder from Urls in CSV file in Python 使用带有图像标签的 csv 文件从文件夹中获取特定图像 - Getting specific images from a folder using a csv file with the labels of the images 从 URL 下载文件并将其保存在 Python 文件夹中 - Download file from URL and save it in a folder Python 如何将csv文件保存到特定文件夹? - How to save csv file to specific folder? 如何从 url 列表中抓取图像并将其下载到本地文件夹? - How to I scrape images from a list of urls and download it to local folder? 从本地文件夹下载文件 - Download file from local folder 如何在 AWS EC2 上运行 Python 代码并将 csv 文件从服务器写入我的本地计算机? - How to run a Python code on AWS EC2 and write a csv file from the server to my local machine? 如何从 url 保存图像并将其保存在特定文件夹中 - how to save an image from a url and save it in a specific folder 如何将数据保存到特定文件夹并在csv文件中添加标题行 - How to save data to a specific folder and add header line in csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM