pandas: powerful Python data analysis toolkit
官方文档: http://pandas.pydata.org/pandas-docs/stable/
1. 导入包pandas
import pandas as pd
2. 获取文件夹下文件名称
import os filenames=[]path="C:/Users/Forrest/PycharmProjects/test" for file in os.listdir(path): filenames.append(file)
3. 读前几行文件(.csv文件)
# -*- coding: utf-8 -*- ##读前几行文件 f= open("C:/Users/Forrest/PycharmProjects/test/train.csv") for i in range(5): print(f.readline().strip())
4. 读取文件和写文件(.csv文件)
file = pd.read_csv("./非空记录/"+filenames[i],sep=‘--->‘,header=None) file.to_csv("./非空all/alldata.csv",header=False,mode=‘a‘,index=False) #a追加
5. pickle读入与写入
import cPickle as pickle output = file(‘./temp.pkl‘, ‘wb‘) pickle.dump(t, output, True) output.close() f2 = file(‘temp.pkl‘, ‘rb‘) a2 = pickle.load(f2) f2.close()
原文地址:https://www.cnblogs.com/Allen-rg/p/8889232.html
时间: 2024-11-02 02:46:48