本文要点刚要:

(一)读文本文件格式的数据函数:read_csv,read_table

1.读不同分隔符的文本文件,用参数sep

2.读无字段名(表头)的文本文件 ,用参数names

3.为文本文件制定索引,用index_col

4.跳行读取文本文件,用skiprows

5.数据太大时需要逐块读取文本数据用chunksize进行分块。

(二)将数据写成文本文件格式函数:to_csv

范例如下:

(一)读取文本文件格式的数据集

1.read_csv和read_table的区别:

#read_csv默认读取用逗号分隔符的文件,不需要用sep来指定分隔符

import pandas as pd

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.csv')

#read_csv如果读的是用非逗号分隔符的文件,必须要用sep指定分割符,不然读出来的是原文件的样子,数据没被分割开

import pandas as pd

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt')

#与上面的例子可以对比一下区别

import pandas as pd

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|')

#read_table读取文件时必须要用sep来指定分隔符,否则读出来的数据是原始文件,没有分割开。

import pandas as pd

pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.csv')

#read_table读取数据必须指定分隔符

import pandas as pd

pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|')

2.读取文本文件时不用header和names指定表头时,默认第一行为表头

#用header=None表示数据集没有表头,会默认用阿拉伯数字填充表头和索引

pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|',header=None)

#用names可以自定义表头

pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|',

names=['x1','x2','x3','x4','x5'])

3.默认用阿拉伯数字指定索引;用index_col指定某一列作为索引

names=['x1','x2','x3','x4','x0']

pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|',

names=names,index_col='x0')

4.以下示例是用skiprows将hello对应的行跳过后读取其他行数据,不管首行是否作为表头,都是将表头作为第0行开始数

可以对比一下三个例子的区别进行理解

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data1.txt')

names=['x1','x2','x3','x4','x0']

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data1.txt',names=names,

skiprows=[0,3,6])

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data1.txt',

skiprows=[0,3,6])

pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data1.txt',header=None,

skiprows=[0,3,6])

5.分块读取,data1.txt中总共8行数据,按照每块3行来分,会读3次,第一次3行,第二次3行,第三次1行数据进行读取。

注意这里在分块的时候跟跳行读取不同的是,表头没作为第一行进行分块读取,可通过一下两个例子对比进行理解。

chunker = pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data1.txt',chunksize=3)

for m in chunker:

print(len(m))

print m

chunker = pd.read_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data1.txt',header=None,

chunksize=3)

for m in chunker:

print(len(m))

print m

(二)将数据写入文本格式用to_csv

以data.txt为例,注意写出文件时,将索引也写入了

data=pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|')

print data

#可以用index=False禁止索引的写入。

data=pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|')

data.to_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\outdata.txt',sep='!',index=False)

#可以用columns指定写入的列

data=pd.read_table('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\data.txt',sep='|')

data.to_csv('C:\Users\xiaoxiaodexiao\pythonlianxi\test0424\outdata2.txt',sep=',',index=False,

columns=['a','c','d'])

python读txt文件-python读取文本文件数据相关推荐

  1. Python创建txt文件并读取与写入

    Python创建txt文件并读取与写入 [PyCharm 软件内操作] 在PyCharm中创建工作目录(D:\python\test1\) 一.创建一个txt文档 # -*- coding: utf- ...

  2. python读txt文件 数组-python将txt等文件中的数据读为numpy数组的方法

    实际中,很多数据都是存为txt文件.csv文件等,但是在程序中处理的时候numpy数组或列表是最方便的.本文简单介绍读入txt文件以及将之转化为numpy数组或列表的方法. 1 将txt文件读为lis ...

  3. python读txt文件时末尾许多空格-python读取txt文件时去掉空格的方法

    python读取txt文件时去掉空格的方法 发布时间:2020-09-22 14:15:44 来源:亿速云 阅读:85 作者:小新 这篇文章给大家分享的是有关python读取txt文件时去掉空格的方法 ...

  4. python分析txt文件_python解析文本文件

    python解析的文件原始数据: 汉皇重色思倾国,御宇多年求不得. 杨家有女初长成,养在深闺人未识. 天生丽质难自弃,一朝选在君王侧. 回眸一笑百媚生,六宫粉黛无颜色. 春寒赐浴华清池,温泉水滑洗凝脂 ...

  5. python读视频文件_python读取和保存视频文件

    如何用python实现视频关键帧提取并保存为图片?也许你会觉得对小编多做一点事你会觉得你很爽,可是在小编看来这是不屑的 import cv2vc = cv2.VideoCapture('Test.av ...

  6. Python从txt文件中提取特定数据

    本段代码用于,想要从一段txt文件中只提取目标数据的情况. 代码: def get_data(txt_path: str = '', epoch: int = 100, target: str = ' ...

  7. python读txt文件报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode

    python读取文件时提示"UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal m ...

  8. python读txt写入excel_Python读取txt内容写入xls格式excel中的方法

    import xlwt import codecs def Txt_to_Excel(inputTxt,sheetName,start_row,start_col,outputExcel): fr = ...

  9. python创建txt文件-python创建txt文件

    1.自己写入txt 直接上核心代码:with open("douban.txt","w") as f: f.write("这是个测试!")1 ...

  10. python读取txt文件如何按数字分隔_python 读txt文件,按‘,’分割每行数据操作

    按行读取TXT文件 fname = './新建文件夹/yob2010.txt' //文件夹路径 with open(fname,'r+',encoding='utf-8') as f: for lin ...

最新文章

  1. pku The Windy's KM最小权匹配 or 最小费用最大流
  2. Xianfeng轻量级Java中间件平台:用户管理
  3. WCF揭秘学习笔记(5):WF定制活动
  4. python真正实现多线程_python多线程实现
  5. java finereport_java报表FineReport_JS整理
  6. gRPC-rs:从 C 到 Rust 1
  7. 有向图的邻接表描述 c++
  8. 关于SAP的视图类型
  9. Oracle发送邮件存储过程
  10. shell:概述、脚本编写、变量的简单基础
  11. 桌面美化 Python tkinter倒计时工具
  12. OPPO Enco X无线降噪耳机体验评测,加钱变强!
  13. JavaScript获取当前时区 时间转换 (实用)
  14. SAP FAGLGVTR 总账科目年末结转
  15. Docker - 相较于虚拟机的优势,以及为什么要使用docker
  16. 解决Android Studio无法下载
  17. android+打开apk文件怎么打开,安卓手机apk文件怎么打开
  18. 一个技术人员的MacOS下环境装备
  19. linux服务器监控工具
  20. Lytro的教训:曾经的光场神器为何不行了?

热门文章

  1. Zynga和Unity:独家奖励广告
  2. Zynga就收购顶尖移动设备游戏《Toon Blast》和《Toy Blast》的开发商伊斯坦布尔Peak公司达成协议
  3. Pine脚本 | 如何编写移动平均线?(附案例)
  4. Zerotier+Microsoft远程桌面 实现内网穿透搭建异地局域网内远程连接控制桌面
  5. Microsoft Edge浏览器黑色背景修改
  6. jscript php,JavaScript_JScript 运算符,JScript 运算符算术运算符- phpStudy
  7. Python初学者之路--range函数、切片、if-elif语句
  8. win10无法打开设置,桌面右键显示设置提示ms-settings:display 该文件没有与之关联的程序来执行该操作
  9. java摄氏度转华氏度 在控制台输入
  10. CSS特效--图像悬停效果