Python converts Excel files to CSV format in batches

Preface: When we use Python to process some tables, we often prefer the CSV format. Maybe I think so, because Python comes with a csv library, and the speed of loading and reading a relatively large amount of data is still very fast ( The amount of data of hundreds of thousands is only a few seconds), the code is as follows:

Python reads csv, each row is a list

import csv

# 打开 CSV 文件
with open('文件路径/文件名.csv', newline='', encoding='utf-8') as csvfile:
    # 读取 CSV 文件内容
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    # 遍历 CSV 文件中的每一行数据
    for row in reader:
        # 处理每一行数据
        print(row)

Well, sometimes most of the files in our office are in Excel format. In fact, Excel format is not impossible to process, but whether you use openpyxl, or xlwings, or pandas, if you encounter hundreds of thousands of table loading It will be very slow, and interested friends can check out the articles I wrote before

Click to jump: xlwings reads excel files, each row lists

Pandas reads Excel, writes too much, everyone finds what they need

insert image description here

Excel to csv, first check whether the video is the effect you want:

Please add a picture description

Provide the full version of the code


# xlsx文件转csv文件
import os,time
import pandas as pd  # 导入pands库


def xlsx2csv():
    # 读取xlsx数据
    t1 = time.time()
    for f in os.listdir("./数据源/"):
        data = pd.read_excel("./数据源/" + f, index_col=0)  # 设置index_col=0,写入文件时第一列不会存在序列号
        data.to_csv("./转换结果/" + f + '.csv', encoding='utf-8')  # 将数据写入csv文件
        print("写入完成......")
        t2 = time.time()
        print(t2 - t1)

xlsx2csv()


I hope everyone has to help

A little programmer dedicated to office automation#

I've seen this, follow + like + bookmark = don't get lost! !

If you want to know more about Python office automation, please pay attention!

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/130819843