zoukankan      html  css  js  c++  java
  • 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("
    ")
    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:UsersDesktopmatlab1026	raindata1.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:UsersDesktopmatlab1026	raindata1.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:UsersDesktopmatlab.xlsx")
     7 sh = wb["b"]
     8 with open("C:UsersDesktopmatlab	raindata1.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("
    ")
    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("
    ")
    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])+';')
  • 相关阅读:
    android开发环境搭建日记和嵌入式Android开发环境初探
    QT210 android2.3 和android4.0 烧写编译日记
    RAM,SRAM,DRAM,SDRAM,DDR RAM,ROM,PROM,EPROM,EEPROM,NAND FLASH,NOR FLASH的区别
    如何修改终端用户名颜色修改为红色(可以自由定制)
    FPGA机器学习之stanford机器学习第三堂2
    FPGA机器学习之stanford机器学习第三堂1
    FPGA机器学习之stanford机器学习第二堂2
    FPGA机器学习之stanford机器学习第二堂1
    FPGA机器学习之stanford机器学习第一堂
    FPGA片外存储器ddr2之DQSn引脚的分析
  • 原文地址:https://www.cnblogs.com/zx-zhang/p/9942640.html
Copyright © 2011-2022 走看看