Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python reads the picture name of the picture in the folder and writes it to the excel table

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how python reads the picture names of the pictures in the folder and writes them into the excel table. The content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Sometimes, we need to read the picture name and write it into the table in order to combine other information of the picture for further analysis.

If you want to read the origin_file folder on the E disk now, read the picture name inside and write it to the excel file img.xlsx.

First, you need to read the picture folder path

Import pandas as pdimport os os.chdir ('E:\') # 1. Read the picture folder path path='origin_file'

Then, pandas creates a blank excel file "img.xlsx"

# 2. Create a blank excel file "img.xlsx" writer=pd.ExcelWriter ("img.xlsx")

Then, iterate through the files in the pictures folder and write the file name to the new list

# 3. Write the file name in the picture folder to the new list # 3.1 traversing the picture folder for root,dirs,files in os.walk (path): # os.walk () method is an easy-to-use file and directory traversal, which can help us deal with files and directories efficiently. # root indicates the folder path currently being accessed # dirs indicates the subdirectory name list# files under this folder indicates the files under this folder listlist= [] # create a new list list#3.2 traversal file all the picture files in the list are written to the new list list for file in files: file=file.rstrip (".jpg") # write the figure The ".jpg" at the end of the title removes list.append (file) # and adds the picture name to the new list list

Then, nest the list list into the dictionary data, convert it to dataframe format and store it in the excel you initially created

# 4. Nest the list list into the dictionary data dict_= {'filename':list} # key name is the field name of the new table, and the value is the list # 5 with the picture name as the element. Convert to dataframe format df=pd.DataFrame (dict_) # 6. Stored in the initial excel df.to_excel (writer,'sheet1',startcol=0,index=False) # worksheet name is "sheet1", starting as the first column, without the need for index # 7. Save the file writer.save ()

The picture names of the pictures in the folder are written into the excel table.

Summary of ideas:

Read destination folder-create a new excel-- picture name write list-list nested dictionary to dataframe format-save in excel

The complete code is as follows: import pandas as pdimport os os.chdir ('E:\\') # 1. Read the picture folder path path='origin_file'#2. Create a blank excel file "img.xlsx" writer=pd.ExcelWriter ("img.xlsx") # 3. Write the file name in the picture folder to the new list # 3.1 traversing the picture folder for root,dirs,files in os.walk (path): # os.walk () method is an easy-to-use file and directory traversal, which can help us deal with files and directories efficiently. # root indicates the folder path currently being accessed # dirs indicates the subdirectory name list# files under this folder indicates the files under this folder listlist= [] # create a new list list#3.2 traversal file all the picture files in the list are written to the new list list for file in files: file=file.rstrip (".jpg") # write the figure ".jpg" at the end of the title remove list.append (file) # add the picture name to the new list list # 4. Nest the list list into the dictionary data dict_= {'filename':list} # key name is the field name of the new table, and the value is the list # 5 with the picture name as the element. Convert to dataframe format df=pd.DataFrame (dict_) # 6. Stored in the initial excel df.to_excel (writer,'sheet1',startcol=0,index=False) # worksheet name is "sheet1", starting as the first column, without the need for index # 7. Save the file writer.save () the above is how python reads the picture names of the pictures in the folder and writes them into the excel table. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report