亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何阻止 Excel 工作表被覆蓋,我希望它在一張工作表中

如何阻止 Excel 工作表被覆蓋,我希望它在一張工作表中

Cats萌萌 2023-06-27 16:25:18
當數據被寫入(for循環)在一個Excel工作表中時,它會覆蓋Excel工作表,而且為了阻止它覆蓋,我需要將收集到的新數據分離到工作表中。(熊貓)那么我該怎么做呢?代碼如下:ih = input('pages: ')def test():        for page in range(1, int(ih)):        req = requests.get(url + str(page))        soup = BeautifulSoup(req.content, 'html.parser')        g_data = soup1.find_all('span', {"class": "b-card b-card-mod-h vehicle"})        g_price = soup.find_all('div', {"class": "b-card--el-vehicle-price"})        g_mile = soup.find_all('p', {"class": "b-card--el-brief-details"})        g_name = soup.find_all('p', {"class": "b-card--el-description"})        g_user = soup.find_all('a', {"class": "b-card--el-agency-title"})        g_link = soup.find_all('div', {"class": "b-card--el-inner-wrapper"})        m_price = [item.text for item in g_price]        m_mile = [item.text for item in g_mile]        m_user = [item.text for item in g_user]        m_name = [item.text for item in g_name]        m_link = [item.a["href"] for item in g_link]        m_extensions = [('') for item in g_link]        l1 = m_name        l2 = m_mile        l3 = m_price        l4 = m_user        l5 = m_link        l6 = m_extensions        s1 = pd.Series(l1, name='Vehicle Name')        s2 = pd.Series(l2, name='Mileage')        s3 = pd.Series(l3, name='Price')        s4 = pd.Series(l4, name='User')        s5 = pd.Series(l5, name='Link')        s6 = pd.Series(l6, name='Site')        df = pd.concat([s1,s2,s3,s4,s6+s5], axis=1)        if(os.path.isfile('hello_world.xlsx')):            sheet.write(df)            workbook.close()        else:            sheet.write('hello_world.xlsx', index= False)            workbook.close()        print(f'[+]Writing Data from page ' + str(page))        ctypes.windll.kernel32.SetConsoleTitleW('[+]Writing Data from page ' + str(page))    print('[=]Written Data')# Write the data.test()如果有人可以幫忙,謝謝!
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

您可以使用 openpyxl 獲取工作表的最后一行,然后使用 dataframeto_excel方法將數據寫入特定行。請注意,您必須設置writer.sheets為防止在保存之前清除工作簿。


將此方法添加到您的代碼中:


def AppendExcel(df, filename):

    import openpyxl

    sheetname = "Sheet1"

    if not os.path.isfile(filename):  # create new file

        df.to_excel(filename, startrow=0, index=False, sheet_name=sheetname) 

    else:  # append

        wb = openpyxl.load_workbook(filename)

        writer = pd.ExcelWriter(filename, engine='openpyxl') 

        writer.book = wb

        writer.sheets = dict((ws.title, ws) for ws in wb.worksheets) # need this to prevent overwrite

        lastrow = wb[sheetname].max_row

        df.to_excel(writer, startrow=lastrow, index=False, header=False, sheet_name=sheetname) 

        writer.save()

有了這個:


AppendExcel(df, 'hello_world.xlsx')

此代碼未經測試,因此您可能需要對其進行一些調整。


查看完整回答
反對 回復 2023-06-27
  • 1 回答
  • 0 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號