编程要求
根据提示,在右侧编辑器补充代码。constants.txt
文件保存在src/step1/constants.txt
路径下,文件包含了一张表格,给出了物理学中一些基本常量的值和维度。将这个表格加载到名为constants
字典中,其中以常量的名称作为键,该常量的值作为键值。例如: 可根据constants['gravitational constant']
来得到牛顿万有引力定律中的万有引力常数6.67259×10-11
。请创建一个读取和解析文件中文本的函数,最后打印该字典。
其中constants.txt
文件中的内容为:
测试说明
平台会对你编写的代码进行测试,测试预期的输出为:
{'speed of light': 299792458.0,
'gravitational constant': 6.67259e-11,
'Planck constant': 6.6260755e-34,
'elementary charge': 1.60217733e-19,
'Avogadro number': 6.0221367e+23,
'Boltzmann constant': 1.380658e-23,
'electron mass': 9.1093897e-31,
'proton mass': 1.6726231e-27}
开始你的任务吧,祝你成功!
#coding=utf-8
import re
# 请在此处填写代码
#********** Begin **********#
f =open('src/step1/constants.txt','r')
constants = {}
count =0
for i in f:
count=count+1
if count>2:
j=re.split('\s{2,}',i)
constants[j[0]]=float(j[1])
print(constants)
#********** End **********#