Python文件存读取

Python文件存读取

想整理一下存读取函数,方便以后直接调用。

读取

1、读xls、csv、xlsx到dataframe

这段代码非常好用,这个函数直接把各种格式文件给汇总了

def readDataFile(readPath):  # readPath: 数据文件的地址和文件名
    try:
        if (readPath[-4:] == ".csv"):
            dfFile = pd.read_csv(readPath, header=0, sep=",")  # 间隔符为逗号,首行为标题行
            # dfFile = pd.read_csv(filePath, header=None, sep=",")  # sep: 间隔符,无标题行
        elif (readPath[-4:] == ".xls") or (readPath[-5:] == ".xlsx"):  # sheet_name 默认为 0
            dfFile = pd.read_excel(readPath,header=0)  # 首行为标题行
            # dfFile = pd.read_excel(filePath, header=None)  # 无标题行
        elif (readPath[-4:] == ".dat"):  # sep: 间隔符,header:首行是否为标题行
            dfFile = pd.read_table(readPath, sep=" ", header=0)  # 间隔符为空格,首行为标题行
            # dfFile = pd.read_table(filePath,sep=",",header=None) # 间隔符为逗号,无标题行
        else:
            print("不支持的文件格式。")
    except Exception as e:
        print("读取数据文件失败:{}".format(str(e)))
        return
    return dfFile

存入

字典逐行存入csv

    kind_num_dict={1:0,2:0,3:0,4:2}
    mid = pd.DataFrame(list(kind_num_dict.items()))
    mid.to_csv('./p1_不同单品累加求和.csv', header=False, index=False)

列表中的字典元素逐行存入xlsx

xlsx_list=[{'日期':'2020-07-01','值':35},{'日期':'2020-07-02','值':31},{'日期':'2020-07-03','值':54}]
import pandas as pd

#定义一个字典


#将字典格式化为DataFrame数据

data = pd.DataFrame(xlsx_list)

#将数据写入Excel中

data.to_excel('test.xlsx')

你可能感兴趣的:(python,python,开发语言)