[英]Interpreting Strings as Other Data Types in Python
I'm reading a file into python 2.4 that's structured like this: 我正在将文件读入python 2.4,其结构如下:
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. 我们的想法是将其解析为一个字典,该字典将fieldfoo
作为键,并将冒号后面的任何内容作为值。
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!"
我想将冒号之后的任何内容转换为它的“实际”数据类型,也就是说, '7'
应该转换为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? python标准库中是否有一个函数可以让人轻松进行这种转换?
The only things this should be used to parse were written by me, so (at least in this case) safety is not an issue. 应该用来解析的唯一内容是我写的,所以(至少在这种情况下)安全不是问题。
First parse your input into a list of pairs like fieldN: some_string
. 首先将输入解析为对象列表,如fieldN: some_string
。 You can do this easily with re
module, or probably even simpler with slicing left and right of the index line.strip().find(': ')
. 您可以使用re
模块轻松完成此操作,或者甚至可以更简单地使用索引line.strip().find(': ')
左右切片。 Then use a literal eval on the value some_string
: 然后在值some_string
上使用文字eval:
>>> 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'>
For older python versions, like the one being asked, the eval
function can be used but, to reduce evilness, a dict
to be the global namespace should be used as second argument to avoid function calls. 对于较旧的python版本,就像被问到的那样,可以使用eval
函数,但为了减少邪恶,应该将dict
作为全局命名空间作为第二个参数来避免函数调用。
>>> [eval(i, {"__builtins__":None}) for i in ['6.2', '"Hello, world!"', '7']]
[6.2, 'Hello, world!', 7]
You can attempt to convert it to an int
first using the built-in function int()
. 您可以尝试使用内置函数int()
将其转换为int
。 If the string cannot be interpreted as an int a ValueError
exception is raised. 如果字符串不能解释为int,则引发ValueError
异常。 You can then attempt to convert to a float
using float()
. 然后,您可以尝试使用float()
转换为float
。 If this fails also then just return the initial string 如果失败也只返回初始字符串
def interpret(val):
try:
return int(val)
except ValueError:
try:
return float(val)
except ValueError:
return val
Since the "only data types that need to be parsed are int
, float
and str
" , maybe somthing like this will work for you: 由于“只需要解析的数据类型是int
, float
和str
” ,也许这样的事情对你有用:
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}
There is strconv lib. 有strconv lib。
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
Hope this helps to do what you are trying to do: 希望这有助于做你想做的事情:
#!/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
Thanks to wim for helping me figure out what I needed to search for to figure this out. 感谢wim帮我弄清楚我需要搜索什么来解决这个问题。
One can just use eval()
: 可以使用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!'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.