Python應(yīng)用實(shí)現(xiàn)處理excel數(shù)據(jù)過(guò)程解析
實(shí)現(xiàn)功能
excel表格中有4列數(shù),分別為RMF計(jì)算得到的 β,γ,勢(shì)能面及組態(tài),需要挑選出相同 β 值下勢(shì)能面最低時(shí)的組態(tài)。為了減小數(shù)據(jù)量,先將 β 值保留兩位小數(shù)。
代碼
import xlrdimport xlwt# read xls filereadfile = xlrd.open_workbook(’./beta-gamma-constrain.xlsx’)readsheet = readfile.sheet_by_name(’Sheet1’)beta = readsheet.col_values(0)gamma = readsheet.col_values(1)energy = readsheet.col_values(2)config = readsheet.col_values(3)’’’print(beta)print(gamma)print(energy)print(config)’’’beta_2f = [round(x, 2) for x in beta]beta_gamma = dict(zip(beta_2f, gamma))beta_energy = dict(zip(beta_2f, energy))beta_config = dict(zip(beta_2f, config))for i in range(0, len(beta_2f)): if energy[i] < beta_energy[beta_2f[i]]: beta_gamma[beta_2f[i]] = gamma[i] beta_energy[beta_2f[i]] = energy[i] beta_config[beta_2f[i]] = config[i] else: continueprint(beta_gamma)print(beta_energy)print(beta_config)# write xls filewrite_excl = xlwt.Workbook(encoding=’utf-8’)excl_sheet = write_excl.add_sheet(’Sheet1’)j = 0for key, value in beta_gamma.items(): excl_sheet.write(j, 0, key) excl_sheet.write(j, 1, value) excl_sheet.write(j, 2, beta_energy[key]) excl_sheet.write(j, 3, beta_config[key]) j = j+1write_excl.save('xx.xls')
用到的庫(kù)
xlrd,讀取 excel 文件的庫(kù),可以讀取 xls 和 xlsx 文件。
xlwt,寫入 excel 文件的庫(kù),只能寫成 xls 文件。
思路
將數(shù)據(jù)按列讀出,寫入 4 個(gè)列表,再組裝為字典。由于字典中的 key 值是唯一的,因此該過(guò)程只是得到了 β-勢(shì)能面的字典,但勢(shì)能面的值不是最小的,需要遍歷判斷再賦值。最后將結(jié)果寫入新的 excel 表格。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp文件如何打開2. Spring依賴注入的三種方式實(shí)例詳解3. JSP出現(xiàn)中文亂碼問(wèn)題解決方法詳解4. 怎樣打開XML文件?xml文件如何打開?5. ASP基礎(chǔ)入門第二篇(ASP基礎(chǔ)知識(shí))6. ASP.NET MVC限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù)7. ASP和PHP文件操作速度的對(duì)比8. ASP.NET MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面9. jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法10. jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄
