5

I'm reading a file into python 2.4 that's structured like this:

field1: 7
field2: "Hello, world!"
field3: 6.2

The idea is to parse it into a dictionary that takes fieldfoo as the key and whatever comes after the colon as the value.

I want to convert whatever is after the colon to it's "actual" data type, that is, '7' should be converted to an int, "Hello, world!" to a string, etc. The only data types that need to be parsed are ints, floats and strings. Is there a function in the python standard library that would allow one to make this conversion easily?

The only things this should be used to parse were written by me, so (at least in this case) safety is not an issue.

4

8 回答 8

6

首先将您的输入解析为一对列表,例如fieldN: some_string. 您可以使用remodule 轻松完成此操作,或者使用 index 的左右切片甚至更简单line.strip().find(': ')。然后在 value 上使用文字 eval some_string

>>> import ast
>>> ast.literal_eval('6.2')
6.2
>>> type(_)
<type 'float'>
>>> ast.literal_eval('"Hello, world!"')
'Hello, world!'
>>> type(_)
<type 'str'>
>>> ast.literal_eval('7')
7
>>> type(_)
<type 'int'>
于 2012-01-31T00:35:12.873 回答
2

对于较旧的 python 版本,就像被问到的那样,eval可以使用该函数,但是为了减少邪恶,dict应该使用作为全局命名空间的 a 作为第二个参数以避免函数调用。

>>> [eval(i, {"__builtins__":None}) for i in ['6.2', '"Hello, world!"', '7']]
[6.2, 'Hello, world!', 7]
于 2012-01-31T00:50:44.573 回答
2

您可以尝试int使用内置函数将其转换为第一个int()。如果字符串不能被解释为 int,ValueError则会引发异常。然后,您可以尝试转换为floatusing float()。如果这也失败了,那么只返回初始字符串

def interpret(val):
    try:
        return int(val)
    except ValueError:
        try:
            return float(val)
        except ValueError:
            return val
于 2012-01-31T00:56:16.173 回答
1

由于“唯一需要解析的数据类型是int,floatstr,也许这样的东西对你有用:

entries = {'field1': '7', 'field2': "Hello, world!", 'field3': '6.2'}

for k,v in entries.items():
    if v.isdecimal():
        conv = int(v)
    else:
        try:
            conv = float(v)
        except ValueError:
            conv = v
    entries[k] = conv

print(entries)
# {'field2': 'Hello, world!', 'field3': 6.2, 'field1': 7}
于 2012-01-31T00:57:35.867 回答
1

您可以使用它yaml来解析文字,ast如果字符串没有围绕额外的撇号或引号对,它不会引发错误。

>>> import yaml
>>> yaml.safe_load('7')
7
>>> yaml.safe_load('Hello')
'Hello'
>>> yaml.safe_load('7.5')
7.5
于 2022-02-18T03:11:16.613 回答
0

感谢wim帮助我弄清楚我需要搜索什么来解决这个问题。

可以只使用eval()

>>> a=eval("7")
>>> b=eval("3")
>>> a+b
10
>>> b=eval("7.2")
>>> a=eval("3.5")
>>> a+b
10.699999999999999
>>> a=eval('"Hello, "')
>>> b=eval('"world!"')
>>> a+b
'Hello, world!'
于 2012-01-31T00:57:35.503 回答
0

希望这有助于做你想做的事情:

#!/usr/bin/python

a = {'field1': 7}
b = {'field2': "Hello, world!"}
c = {'field3': 6.2}

temp1 = type(a['field1'])
temp2 = type(b['field2'])
temp3 = type(c['field3'])

print temp1
print temp2
print temp3
于 2012-01-31T00:40:34.123 回答
0

strconv库。

In [22]: import strconv
/home/tworec/.local/lib/python2.7/site-packages/strconv.py:200: UserWarning: python-dateutil is not installed. As of version 0.5, this will be a hard dependency of strconv fordatetime parsing. Without it, only a limited set of datetime formats are supported without timezones.
  warnings.warn('python-dateutil is not installed. As of version 0.5, '

In [23]: strconv.convert('1.2')
Out[23]: 1.2

In [24]: type(strconv.convert('1.2'))
Out[24]: float

In [25]: type(strconv.convert('12'))
Out[25]: int

In [26]: type(strconv.convert('true'))
Out[26]: bool

In [27]: type(strconv.convert('tRue'))
Out[27]: bool

In [28]: type(strconv.convert('12 Jan'))
Out[28]: str

In [29]: type(strconv.convert('12 Jan 2018'))
Out[29]: str

In [30]: type(strconv.convert('2018-01-01'))
Out[30]: datetime.date
于 2018-05-09T17:56:36.327 回答