Teach you how to use Python to easily split Excel into multiple CSV files

Click on " Python crawler and data mining " above to follow

Reply to " Books " to receive a total of 10 e-books of Python from beginner to advanced

now

day

Chickens

soup

Passionate but seeming to always be merciless, but I feel that I can't laugh in front of him.

I. Introduction

In daily life or work, we occasionally encounter such a confusing situation-when a single Excel file is large or needs to be split into multiple CSV files according to the content of a certain column, use Excel's The screening function is feasible to screen slowly, but the workload of repeated back and forth is relatively large. But don’t panic, friends, in fact, we only need a few lines of Python code to achieve this situation! Let's take a look~

2. Project goals

Split a single Excel file into multiple CSV files or split into multiple CSV files based on the content of a column.

3. Project preparation

Software: PyCharm

Required libraries: tkinter, pandas, xlrd

Four, project analysis

1) How to choose the target file?

Use the filedialog in the tkinter library to select the target file to be processed.

2) How to read Excel files?

Use the xlrd library to read in Excel and obtain the target Sheet to be processed.

3) How to filter column content?

Use the pandas library to filter the contents of the columns to obtain the split data.

4) How to save the file?

Use to_csv() to save the data and get the final split target file.

Five, project realization

1. The first step is to import the required libraries

from tkinter import filedialog
import pandas as pd
import xlrd

2. Select the target file in the second step

path = filedialog.askopenfilename().replace('/', '\\')
first_file_fh=xlrd.open_workbook(path)
# 选择要拆分的文件
first_file_sheet=first_file_fh.sheets()

3. The third step is to read the Excel file

  
for sheetname in first_file_sheet:
      sheet_names.append(sheetname.name)
  df = pd.read_excel(path, sheet_name=sheet_names[0])

4. The fourth step is to filter and save according to the content of a certain column

 for c in list_c:
      # 根据列的内容循环读取
      df2=df[df['地市']==c]
      # 根据列的内容进行筛选
      df2.to_CSV('./excel_CSV/auto_ok/32_'+c+'.CSV', encoding='gbk',index=None)
      # 筛选后的内容保存为CSV

Six, effect display

1. Excel data before processing:

2. The CSV file save results after the final split:

3. In order to make it easier for everyone to operate, the editor recorded a small video, welcome everyone to start and follow the practice, if you feel good, remember to like it~

Seven, summary

This article introduces how to use Python to split an Excel file so that it can be split according to the content of any column. It's easy to achieve in a few lines of Python code, it's fun.

Finally, those who need the project code of this article, please reply to the " split file " keyword in the backstage of the official account to obtain it. If you encounter any problems during the operation, please feel free to leave a message or add a friend to the editor. The editor will see it. Help everyone to solve the bug!

------------------- End -------------------

Recommendations of previous wonderful articles:

Welcome everyone to like , leave a message, forward, reprint, thank you for your company and support

If you want to join the Python learning group, please reply in the background [ Enter the group ]

Thousands of rivers and mountains are always in love, can you click [ Looking ]

/Today's message topic/

Just say a word or two~~

Guess you like

Origin blog.csdn.net/pdcfighting/article/details/113976557