pandas用read_table读取txt

参数:

filepath_or_buffer :表示文件系统位置、URL、文件型对象的字符串

sep或delimiter : 用于对行中各字段进行拆分的字符序列或正则表达式‘\r','\t',多种分隔符时使用 '\s+',默认为'\t'
header : 用作列名的行号。默认为0(第一行),如果没有header行就应该设置为None

读取没有标题的文件时,默认为第一行作为列标题,列索引为0开始的数字,

设置header=None, 则列索引也变为0开始的数字,通过names=['a','b','c']可以自己设置列标题。

通过index_col可以设置列索引,默认使用0开始的整数为列索引。

filepath=r"C:\Users\zrx\Desktop\ocean_land_tem.txt"
data=pd.read_table(filepath,sep='\s+',header=None)
data_above_sea_ice=data.iloc[:,1:5]
data_below_sea_ice=data.iloc[:,5:]
#np.savetxt("above.csv",np.mat(data_above_sea_ice),delimiter=',')
#np.savetxt("below.csv",np.mat(data_below_sea_ice),delimiter=',')
data_above_sea_ice.to_csv('above.csv', sep=',',header=True, index=False)
data_below_sea_ice.to_csv('below.csv', sep=',',header=True, index=False)

ps:由这篇博客我们可以知道read_csv和read_table除了默认的切割符号不同,其他是一样的

发布了62 篇原创文章 · 获赞 118 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/qq_38412868/article/details/101919036
今日推荐