在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  HTML/ Python怎么讀取Excel的行數(shù)和列數(shù)?

Python怎么讀取Excel的行數(shù)和列數(shù)?

在把數(shù)據(jù)寫入Excel的過程中遇到了問題,寫入的數(shù)據(jù)是for循環(huán)進去的,所以是分多次寫入(每次for循環(huán)寫入一次,一次有幾十條數(shù)據(jù))。
但是在第二次for循環(huán)寫數(shù)據(jù)時,就會覆蓋掉第一次寫的數(shù)據(jù),就是后一次寫入的數(shù)據(jù)會覆蓋掉之前寫的,代碼片段:

    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('sheet')
    biaotou = ['名稱','代碼','時間','開價','高價','低價','收價','漲額','漲幅','成量','成價','振','換率']
    for i in range(0,len(biaotou)):
        worksheet.write(0,i,biaotou[i])
        workbook.save('%s.xlsx' % str(str(cons[0][0]) + '(' + str(cons[0][1]) + ')'))
    open_file = xlrd.open_workbook('%s.xlsx' % str(str(cons[0][0]) + '(' + str(cons[0][1]) + ')'))
    table = open_file.sheets()[0]
    rows_num = table.nrows
    for con in cons:
        rows_num += 1
        print(rows_num)
        for i in range(0,13):
            worksheet.write(rows_num,i,con[i])
            workbook.save('%s.xlsx' % str(str(cons[0][0]) + '(' + str(cons[0][1]) + ')'))

我的解決方法是獲取當前Excel的行數(shù),然后寫入的時候就從行數(shù)+1開始寫數(shù)據(jù),這樣就不會覆蓋掉,但是,但是行號每次for循環(huán)都是從1到60,第二次還是1到60。。。第三次還是1到60。。。講道理每次都讀取行號,應(yīng)該是累加的呀
或者還有其它好的解決方法,pandas...help me

回答
編輯回答
哎呦喂

對簡單數(shù)據(jù)類型,用 pandas 非常方便

# -*- coding: utf-8 -*-
import pandas as pd

df = pd.DataFrame([[1, 2, 3], [10, 20, 30]], columns=['a', 'b', 'c'])
df.to_excel('a.xlsx', 'demo sheet', index=False)
2018年5月29日 23:47