python中txt转成csv_Python处理文本文档(txt/excel/csv)

1.环境配置pip install xlrd

pip install xlwt

pip install csv

安装完成后请在文件头部引用对应库#coding:utf-8

import xlrd

import xlwt

import csv

2.处理非格式化文档

逐行读取:fileHanlder=open('test.txt','r')

for line in fileHanlder:

if(line.find('a')>-1):

print line

fileHanlder.close()

find()方法是字符串内嵌的,检索字符串并返回关键词的位置(第一次出现),未找到关键词则返回-1

逐行写入:word_buffer=['aa','dd','sdfsdf','sdsd']

fileHanlder=open('test.txt','w')

for item in word_buffer:

fileHandler.write(item+'\n')

fileHanlder.close()

3.处理excel文档

读取某个文件某一格的值:workbook = xlrd.open_workbook('caipeipei.xlsx')#打开excel文件

sheet = workbook.sheet_by_name('xianshangyanzheng'.decode('utf-8'))#获取文件的一个表对象,表名必须是unicode编码

print sheet.cell_value(2,1)#打印第3行第2列的值,行列起点都是0

for i in range(0,sheet.nrows): #行数

print sheet.cell_value(i,1)

写入文件:workbook = xlwt.Workbook(encoding = 'utf-8')#打开excel文件

try:

sheet = workbook.add_sheet('测试')

sheet.write(1,1,'啥')#在第2行第2列写入‘啥’

except Exception, e:

print Exception,':',e

finally:#写文件过程往往有业务逻辑,比较长。这么写如果业务逻辑有异常,已经处理好的内容不会丢失。

workbook.save('test.xls')

4.处理csv文档

首先打印文档的前几行,以确认文档是否有列标题:fileHandler=open('test_feedback.csv')

count=0

for line in fileHandler:

if(count>2):

break

print line

count+=1

fileHandler.close()

如果文档没有标题,则只能使用列序号来获取数值:with open('test_feedback.csv', 'rb') as f:# 采用b的方式处理可以省去很多问题

reader = csv.reader((line.replace('\0','') for line in f))#csv库缺陷,有空操作符会报错,因此先替换掉

count=0

for row in reader:

if(count>2):

break

print row[0]#打印每行的第一列值

count+=1

如果文档有标题,则可以用标题取值with open('test_feedback.csv', 'rb') as f:# 采用b的方式处理可以省去很多问题

reader = csv.DictReader((line.replace('\0','') for line in f))#csv库缺陷,有空操作符会报错,因此先替换掉

count=0

for row in reader:

if(count>2):

break

print row['工单号'.decode('utf-8').encode('gbk')]#key的编码需要和csv文件一致

count+=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值