Python——提取excel指定单元格的数据到txt中

2.将excel中指定单元格的数据提取并存储到txt文件中
(1)使用openpyxl的load_workbook模块
        问题:load_workbook只能使用.xlsx文件,不能打开.xls文件。而xlrd可以打开.xlsx文件
        .xlsx使用于2003版以上的excel文件;
        .xls适用于2003以下的excel文件。
        对xlrd模块的介绍( https://www.cnblogs.com/insane-Mr-Li/p/9092619.html
(2)存储为txt文档时的三种方式
            读模式('r')、写模式('w')、追加模式('a')
(3)正确创建一个list
            col1 = []、col1 = [0]*22
(4)对excel中单元格的操作方式        
for index,item in enumerate(sh["C2":"X2"]):
j = 0
    if index > 0:
        print("\n")
for i in item:
    print(i.value,end=" ")
    col1[j] = i.value
    j = j+1

(5)python保留小数点后四位数字的三种方法  

1  print(Decimal('cell.value').quantize(Decimal('0.0000')),end=" ") #使用decimal函数将小数点后保留四位
2  print('%.4f' %cell.value,end=" ") #小数点后保留四位
3 round(cell.value,4) #小数点后保留四位

(6)

TypeError: 'int' object is not iterable

经过多次查询,最终找到原因:不能直接用int进行迭代,而必须加个range。int不能使用迭代器函数iter( )    

1  错误:n=22;for i in n:
2  正确:n=22;  for i in range(n): 

(7)

ValueError : I/O operation on closed file. #在关闭的文件中进行I/O处理

问题:python的严格缩进,强制可读性   

 1 错误:
 2 with open("C:\Users\Desktop\matlab\1026\traindata1.txt","a+") as wr:
 3 for j in range(n):
 4         print(str(col1[j])+','+str(col2[j])+';')
 5         wr.write(str(col1[j])+','+str(col2[j])+';')
 6 正确:
 7 with open("C:\Users\Desktop\matlab\1026\traindata1.txt","a+") as wr:
 8         for j in range(n):
 9                 print(str(col1[j])+','+str(col2[j])+';')
10                 wr.write(str(col1[j])+','+str(col2[j])+';')

(8)

1 TypeError: unsupported operand type(s) for +: 'str' and 'int'
2         问题:
3         错误: wr.write(col1[j]+','+col2[j]+';')
4         正确: wr.write(str(col1[j])+','+str(col2[j])+';')

 

(9)
IndexError: list assignment index out of range
        问题:刚开始时定义list范围过小,定义的l是一个空list,之后却要引用一个l[j]
        错误:l = [0]; l[j] = i.value
        正确:l = [0]*22; l[i] = i.value;

 

 
 
python代码:
 1 from openpyxl import load_workbook
 2 from decimal import *
 3 import xlrd
 4 
 5 #df = xlrd.open_workbook(r"桌面\1.xlsx") #python中的‘\’表示转义字符,可在前面加上r或者使用‘/’
 6 wb = load_workbook(r"C:\Users\Desktop\matlab\b.xlsx")
 7 sh = wb["b"]
 8 with open("C:\Users\Desktop\matlab\traindata1.txt","a+") as wr:
 9 #names = df.sheet_names() #获取Sheet列表名,xlrd模块中专用
10 #print(names)
11 
12     col1 = [0] *22
13     col2 = [0] *22 #创建一个大小为22的list,python中list相当于数组
14     n=22
15 
16     for index,item in enumerate(sh["C2":"X2"]):
17         j = 0
18         if index > 0:
19             print("\n")
20         for i in item:
21             print(i.value,end=" ")
22             col1[j] = i.value 
23             j = j+1
24     for index,item in enumerate(sh["C5":"X5"]):
25         j = 0
26         if index >0:
27             print("\n")
28         for cell in item:
29             #print(Decimal('cell.value').quantize(Decimal('0.0000')),end=" ") #使用decimal函数将小数点后保留四位
30             print('%.4f' %cell.value,end=" ") #小数点后保留四位    
31             col2[j] = round(cell.value,4) #小数点后保留四位 
32             j = j+1
33       
34         j = 0
35     for j in range(n):
36         print(str(col1[j])+','+str(col2[j])+';')
37         wr.write(str(col1[j])+','+str(col2[j])+';')

 

转载于:https://www.cnblogs.com/zx-zhang/p/9942640.html

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值