怎么用python读取txt文件里指定行的内容,并导入excel
1个回答
展开全部
Python code
lnum = 0
with open('pit.txt', 'r') as fd:
for line in fd:
lnum += 1;
if (lnum >= 10) && (lnum <= 13):
print line
fd.close()
def eachlineof(filename):
''' 逐行读取给定的文本文件,返回行号、剔除末尾空字符的行内容 '''
with open(filename) as handle:
for lno, line in enumerate(handle):
yield lno+1, line.strip()
另外: 读写excel需要第三方类库,可以考虑下载安装xlrd, xlwt
lnum = 0
with open('pit.txt', 'r') as fd:
for line in fd:
lnum += 1;
if (lnum >= 10) && (lnum <= 13):
print line
fd.close()
def eachlineof(filename):
''' 逐行读取给定的文本文件,返回行号、剔除末尾空字符的行内容 '''
with open(filename) as handle:
for lno, line in enumerate(handle):
yield lno+1, line.strip()
另外: 读写excel需要第三方类库,可以考虑下载安装xlrd, xlwt
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询