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])+';')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM