怎么用python读取txt文件里指定行的内容,并导入excel

2024-12-04 11:36:41
推荐回答(1个)
回答(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