我需要从values
文件中将常量名称及其对应的.txt
提取到dictionary
。 key = NameOfConstants
和Value=float
。
file
的开头如下:
speed of light 299792458.0 m/s
gravitational constant 6.67259e-11 m**3/kg/s**2
Planck constant 6.6260755e-34 J*s
elementary charge 1.60217733e-19 C
如何轻松获取name
常数?
这是我的尝试:
with open('constants.txt', 'r') as infile:
file1 = infile.readlines()
constants = {i.split()[0]: i.split()[1] for i in file1[2:]}
我没有使用split()
做到正确,我需要稍微纠正一下!
答案 0 :(得分:1)
{' '.join(line.split()[:-2]):' '.join(line.split()[-2:]) for line in lines}
答案 1 :(得分:1)
从您的文本文件中我无法获得正确的空格值以进行拆分。所以下面的代码旨在帮助您。请看一下,它适用于您上述文件。
import string
valid_char = string.ascii_letters + ' '
valid_numbers = string.digits + '.'
constants = {}
with open('constants.txt') as file1:
for line in file1.readlines():
key = ''
for index, char in enumerate(line):
if char in valid_char:
key += char
else:
key = key.strip()
break
value = ''
for char in line[index:]:
if char in valid_numbers:
value += char
else:
break
constants[key] = float(value)
print constants
答案 2 :(得分:0)
您是否尝试过使用正则表达式? 例如
([a-z]|\s)*
匹配一行的第一部分,直到常量的数字开始。
Python提供了一个非常好的正则表达式教程(正则表达式) https://docs.python.org/2/howto/regex.html
您也可以在线试用您的正则表达式 https://regex101.com/
答案 3 :(得分:0)
re.split -
怎么样?import re
lines = open(r"C:\txt.txt",'r').readlines()
for line in lines:
data = re.split(r'\s{3,}',line)
print "{0} : {1}".format(data[0],''.join(data[1:]))
或使用oneliner制作字典 -
{k:v.strip() for k,v in [(re.split(r'\s{3,}',line)[0],''.join(re.split(r'\s{3,}',line)[1:])) for line in open(r"C:\txt.txt",'r').readlines() ]}
输出 -
gravitational constant : 6.67259e-11m**3/kg/s**2
Planck constant : 6.6260755e-34J*s
elementary charge : 1.60217733e-19C
词典 -
{'Planck constant': '6.6260755e-34J*s', 'elementary charge': '1.60217733e-19C', 'speed of light': '299792458.0m/s', 'gravitational constant': '6.67259e-11m**3/kg/s**2'}
答案 4 :(得分:0)
with open('constants.txt', 'r') as infile:
lines = infile.readlines()
constants = {' '.join(line.split()[:-2]):float(' '.join(line.split()[-2:-1])) for line in lines[2:]}
由于上面不需要两行。
答案 5 :(得分:0)
使用正则表达式最好解决这个问题。
专注于您的问题(如何获取姓名)和您的愿望(缩短时间):
import re
# Regular expression fetches all characters
# until the first occurence of a number
REGEXP = re.compile('^([a-zA-Z\s]+)\d.*$')
with open('tst.txt', 'r') as f:
for line in f:
match = REGEXP.match(line)
if match:
# On a match the part between parentheses
# are copied to the first group
name = match.group(1).strip()
else:
# Raise something, or change regexp :)
pass