• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python应用实现处理excel数据过程解析

python 搞代码 4年前 (2022-01-07) 49次浏览 已收录 0个评论

这篇文章主要介绍了Python应用实现处理excel数据过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

实现功能

excel表格中有4列数,分别为RMF计算得到的 β,γ,势能面及组态,需要挑选出相同 β 值下势能面最低时的组态。为了减小数据量,先将 β 值保留两位小数。

代码

 import xlrd import xlwt # read xls file readfile = 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: continue print(beta_gamma) print(beta_energy) print(beta_config) # write xls file write_excl = xlwt.Workbook(encoding='utf-8') excl_sheet = write_excl.add_sheet('Sheet1') j = 0 for 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+1 write_excl.save("xx.xls")

来源gao!daima.com搞$代!码网

用到的库

xlrd,读取 excel 文件的库,可以读取 xls 和 xlsx 文件。

xlwt,写入 excel 文件的库,只能写成 xls 文件。

思路

将数据按列读出,写入 4 个列表,再组装为字典。由于字典中的 key 值是唯一的,因此该过程只是得到了 β-势能面的字典,但势能面的值不是最小的,需要遍历判断再赋值。最后将结果写入新的 excel 表格。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Python应用实现处理excel数据过程解析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python应用实现处理excel数据过程解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址