python读文件每一行|python读取文本内每行指定内容

python读文件每一行|python读取文本内每行指定内容的第1张示图

❶ python读取excel文件,将每一行都保存为一个列表。每一行对应一个列表。

python读写excel文件要用到两个库:xlrd和xlwt,首先下载安装这两个库。

1、#读取Excel

importxlrd

data = xlrd.open_workbook(excelFile)

table = data.sheets()[0]

nrows = table.nrows #行数

ncols = table.ncols #列数

for i in xrange(0,nrows):rowValues= table.row_values(i) #某一行数据for item in rowValues:printitem

2、写Excel文件

'''往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引;

isBold:true:粗字段,false:普通字体'''

defWriteSheetRow(sheet,rowValueList,rowIndex,isBold):

i = 0

style = xlwt.easyxf('font: bold 1')

#style = xlwt.easyxf('font: bold 0, color red;')#红色字体

#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 设置Excel单元格的背景色为黄色,字体为粗体

forsvalue inrowValueList:

strValue = unicode(str(svalue),'utf-8')

ifisBold:

sheet.write(rowIndex,i,strValue,style)

else:

sheet.write(rowIndex,i,strValue)

i = i + 1

'''写excel文件'''

defsave_Excel(strFile):

excelFile = unicode(strFile,"utf8")

wbk = xlwt.Workbook()

sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)

headList = ['标题1','标题2','标题3','标题4','总计']

rowIndex = 0

WriteSheetRow(sheet,headList,rowIndex,True)

fori inxrange(1,11):

rowIndex = rowIndex + 1

valueList = []

forj inxrange(1,5):

valueList.append(j*i)

WriteSheetRow(sheet,valueList,rowIndex,False)

wbk.save(excelFile)

style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')

在设置上Excel单元格的背景色时,fore_colour支持的颜色是有限的,仅支持一下颜色

aqua 0x31black 0x08blue 0x0Cblue_gray 0x36bright_green 0x0Bbrown 0x3Ccoral 0x1Dcyan_ega 0x0Fdark_blue 0x12dark_blue_ega 0x12dark_green 0x3Adark_green_ega 0x11dark_purple 0x1Cdark_red 0x10dark_red_ega 0x10dark_teal 0x38dark_yellow 0x13gold 0x33gray_ega 0x17gray25 0x16gray40 0x37gray50 0x17gray80 0x3Fgreen 0x11ice_blue 0x1Findigo 0x3Eivory 0x1Alavender 0x2Elight_blue 0x30light_green 0x2Alight_orange 0x34light_turquoise 0x29light_yellow 0x2Blime 0x32magenta_ega 0x0Eocean_blue 0x1Eolive_ega 0x13olive_green 0x3Borange 0x35pale_blue 0x2Cperiwinkle 0x18pink 0x0Eplum 0x3Dpurple_ega 0x14red 0x0Arose 0x2Dsea_green 0x39silver_ega 0x16sky_blue 0x28tan 0x2Fteal 0x15teal_ega 0x15turquoise 0x0Fviolet 0x14white 0x09yellow 0x0D"""另外一种方式是 用pyExcelerator

from pyExcelerator import *# excel 第一行数据excel_headDatas = [u'发布时间', u'文章标题', u'文章链接', u'文章简介']articles =[{u'发布时间':u'2017年5月9日',u'文章标题':u'Python项目实战教程:国内就能访问的google搜索引擎',u'u'文章简介':u'大家可以留言、想了解python那个方向的知识、不然我也不知道'},{u'发布时间':u'2017年5月4日',u'文章标题':u'对于学习Django的建议、你知道的有那些',u'文章链接':',u'文章简介':u'随着Django1.4第二个候选版的发布,虽然还不支持Python3,但Django团队已经在着手计划中,据官方博客所说,Django1.5将会试验性的支持python3'}]# 定义excel操作句柄excle_Workbook = Workbook()excel_sheet_name = time.strftime('%Y-%m-%d')excel_sheet = excle_Workbook.add_sheet(excel_sheet_name)index = 0#标题for data in excel_headDatas:excel_sheet.write(0, index, data)index += 1index = 1#内容for article in articles:colIndex = 0 for item in excel_headDatas:excel_sheet.write(index, colIndex, article[item])colIndex += 1index += 1#保存test.xlsx到当前程序目录excle_Workbook.save('test.xlsx')# db = mongoDB.mongoDbBase()# db.Get_information_stat()

❷ python读取文本内每行指定内容

可以参考下面的代码:

f=file(yourpath)

for line in f:

t = line.split("==")

part_1 = t[0] + "=="

(part_2,part_3) = t[1].split("–")

del t

print "第一段:%s第二段:%s第三段:%s" %(part_1,part_2,part_3)

(2)python读文件每一行扩展阅读:

python参考函数

callable(obj) 查看一个obj是不是可以像函数一内样调用容

repr(obj) 得到obj的表示字符串,可以利用这个字符串eval重建该对象的一个拷贝

eval_r(str) 表示合法的python表达式,返回这个表达式

hasattr(obj,name) 查看一个obj的name space中是否有name

setattr(obj,name,value) 为一个obj的name space中的一个name指向vale这个object

❸ python逐行读取txt文件 每行为一个list

#!/usr/bin/envPython#coding=utf-8importre#你的文件路径path="./tags.txt"#读取文件file=open(path,encoding="utf-8")#定义一个用于切割字符串的正则seq=re.compile("s+")result=[]#逐行读取forlineinfile:lst=seq.split(line.strip())item={"name":lst[0],"val":lst[1:]}result.append(item)#关闭文件file.close()print(result)#输出结果类似:[{"name":1,"val":["v1","v2"]},{"name":2,"val":["v1","v2"]}]

❹ python怎样实现一行一行读取文件数据,并且要实现读取一行数据,就进行条件判断

超简单

get='''1215161923'''result=get.split('')foriinresult:ifint(i)>16:print(i)

满意还请采纳

❺ Python中怎么使用for来读取文件中每一行的内容和没一个字符呢

这个是读每行如果你再加一层forfor c in line:print(c)那就是输出每个字符

❻ python 如何读取excel文件 将每一行存为数组

from xlrd import open_workbookwb=open_workbook(r&#39d:/222.xlsx&#39)tb=wb.sheets()[0]data=[]for r in range(tb.nrows): val=[] for c in range(tb.ncols): val.append(tb.cell_value(r,c)) data.append(tuple(val))print(data)

❼ python读取一个txt文件 使其变成每行20个字符的形式

如何让python把从txt文件中读入的文字按20个字符一行的形式分隔开呢。大概的思路是,先读入文件所有字符,然后使用range生成[0,20,40,…]的列表以供索引,然后用这个索引,生成有关于该字符串的新列表[s[0:20],s[20:40],…],若不专门去除换行符,代码用这个思路就够了,可以这么写(以下13行就是)(限于python3):

#-*-coding:utf-8;-*-

f=open("test.txt","w")

print("abcdefghijklmnopqrstabcd"

"efghijklmnopqrstopq",file=f)

f.close()

#生成待读入文件

#再从待读入文件中读入处理

f=open("test.txt","r")

s=f.read()

l=len(s)

b=(list(range(0,l,20)))

p=[s[i:i+20] for i in b]

r=[print(i) for i in p]

按每行20字符的形式输出样例txt

❽ 用python读取文本文件,对读出的每一行进行操作,这个怎么写 操作每一行

“对读出的每一行进行操作”是什么意思?读取文本文件直接用 open(filename) 不就行了,然后用 readlines 逐行读取

❾ Python按行读取文件的简单实现方法

Python按行读取文件的简单实现方法下面小编就为大家带来一篇Python按行读取文件的简单实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break pass # do somethingfile.close()一行一行得从文件读数据,显然比较慢;不过很省内存;测试读10M的sample.txt文件,每秒大约读32000行;2:fileinput import fileinput for line in fileinput.input("sample.txt"): pass写法简单一些,不过测试以后发现每秒只能读13000行数据,效率比上一种方法慢了两倍多;3:readlines() file = open("sample.txt") while 1: lines = file.readlines(100000) if not lines: break for line in lines: pass # do somethingfile.close()用同样的数据测试,它每秒可以读96900行数据!效率是第一种方法的3倍,第二种方法的7倍!4:文件迭代器每次只读取和显示一行,读取大文件时应该这样: file = open("sample.txt") for line in file: pass # do somethingfile.close()以上就是小编为大家带来的Python按行读取文件的简单实现方法全部内容了

❿ python读取文本每行指定内容

f=file(yourpath)for line in f: t = line.split("==") part_1 = t[0] + "==" (part_2,part_3) = t[1].split("–") del t print "第一段:%s\t第二专段:%s\t第三属段:%s" %(part_1,part_2,part_3)

未经允许不得转载:山九号 » python读文件每一行|python读取文本内每行指定内容

赞 (0)