-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_to_txt.py
40 lines (40 loc) · 1.19 KB
/
excel_to_txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#-*- coding:UTF-8 -*-
import xlrd
def strs(row):
"""
:返回一行数据
"""
try:
values = "";
for i in range(len(row)):
if i == len(row) - 1:
values = values + str(row[i])
else:
#使用“,”逗号作为分隔符
values = values + str(row[i]) + ","
return values
except:
raise
def xls_txt(xls_name,txt_name):
"""
:excel文件转换为txt文件
:param xls_name excel 文件名称
:param txt_name txt 文件名称
"""
try:
data = xlrd.open_workbook(xls_name)
sqlfile = open(txt_name, "a")
table = data.sheets()[0] # 表头
nrows = table.nrows # 行数
#如果不需跳过表头,则将下一行中1改为0
for ronum in range(1, nrows):
row = table.row_values(ronum)
values = strs(row) # 条用函数,将行数据拼接成字符串
sqlfile.writelines(values) #将字符串写入新文件
sqlfile.close() # 关闭写入的文件
except:
pass
if __name__ == '__main__':
xls_name = 'G:/test.xls'
txt_name = 'G:/test.txt'
xls_txt(xls_name,txt_name)