标签:
读文件:
传统的读法。所有读出,按行处理:
fp=open("./ps.txt", "r");
alllines=fp.readlines();
fp.close();
for eachline in alllines:
print eachline
推荐读取方法,使用文件迭代器 , 每次仅仅读取和显示一行。读取大文件时应该这样:
fp=open("./ps.txt", "r");
for eachline in fp:
print eachline
使用pandas读写文件:
import pandas as pd
import numpy as np
读文件,转换成np.array:
trainD=pd.read_csv("train.csv")
trainY=np.array(trainD.iloc[:,-1])
trainX=np.array(trainD.iloc[:,1:-1]) #drop ID and TARGET
testD=pd.read_csv("test.csv")
submitID=np.array(testD.iloc[:,0]) #ID column
testX=np.array(testD.iloc[:,1:])#drop ID
写文件,转换为pd.DataFrame:
dataset_trainBlend=np.zeros( ( 3, 2) ) )
DFtrainBlend=pd.DataFrame(dataset_trainBlend)
DFtrainBlend.to_csv("DFtrainBlend3.csv",header=["RFC","GBC"], index=False)
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/5396716.html