怎么用python读取txt文件里指定行的内容,并导入excel
解答如下:
首先打txt文件,使用open(txtName),进行一行一行的读;
如果需要的话,对每行的数据进行;
导入xlrd,xlwt进行excel读写;
大致代码如下:
import xlrd,xlwt
txtName=r"C:\value.txt"
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('sheet1')
fp=open(txtName)
for linea in fp.readlines():
worksheet.write(0, 0, label = linea)
workbook.save('Excel_Workbook.xls')
fp.close()
python怎么把数组数据写入表格文档?举个例子就行
数:1564
[python] view plain copy
arrs=[2,15,48,4,5,6,7,6,4,1,2,3,6,6,7,4,6,8]
f=open('test.txt','w ')
count=0
for temp in arrs:
count =1
print >> f,temp,
if(count%3==0):
print >> f
f.close()
:
Python如何将一个列表写入到一个excel表中的一列,求Python代码,谢啦!
文储是按照每个子列一行的,所以在写文件需要将[10,'haha',0.1]换成[[10],['haha'],[0.1]]。所以只要将mywrite.writerow(i)mywrite.writerow([i])。
import csv
def storFile(data,fileName):
with open(fileName,'w',newline ='') as f:
mywrite = csv.writer(f)
for i in data:
mywrite.writerow([i])
data = [10,'haha',0.1]
storFile(data,"haha.csv")