python如何将excel类型数据转换为txt格式的数据

时间:2024-10-13 02:49:57

1、准备一个Excel文档。这个文档可以到百度文库里面下载,有很多免费的。

python如何将excel类型数据转换为txt格式的数据

2、用xlrd模块打开Excel文档:data = xlrd.open_workbook('00.xls')

python如何将excel类型数据转换为txt格式的数据

3、读取文档里面的所有'sheet':st = data.sheets()有三个sheet。

python如何将excel类型数据转换为txt格式的数据

4、选择第一个sheet:st = data.sheets()【0】

python如何将excel类型数据转换为txt格式的数据

5、st里面只有78行数据:rows = st.nrows

python如何将excel类型数据转换为txt格式的数据

6、读取st的第一行数据:st.row_values(0)

python如何将excel类型数据转换为txt格式的数据

7、读取st的前10行数据:for i in range(rows): if i<10: st.row_values(i)

python如何将excel类型数据转换为txt格式的数据

8、打开一个txt文档,并按行把st里面的所有数据,输入到txt里面:t = open('0.txt','w')for 足毂忍珩i in range(rows): t.write(str(st.row_values(i)))t.close()

python如何将excel类型数据转换为txt格式的数据

9、改为每一行结束,则换行:for i in range(rows): t.write猾诮沓靥(str(st.row_values(i))+'\n')

python如何将excel类型数据转换为txt格式的数据
© 手抄报圈