[英]Iterating a conversion of a string to a float in a scripting file when parsing an old file
I am using a new script (a) to extract information from an old script (b) to create a new file (c). 我正在使用新脚本(a)从旧脚本(b)提取信息以创建新文件(c)。 I am looking for an equal sign in the old script (b) and want to modify the modification script (a) to make it automated. 我正在寻找旧脚本(b)中的等号,并且想要修改修改脚本(a)以使其自动化。
The string is lev1tolev2 'from=e119-b3331l1 mappars="simp:180" targ=enceladus.bi.def.3 km=0.6 lat=(-71.5,90) lon=(220,360)' It is written in python 3. 字符串是lev1tolev2'from = e119-b3331l1 mappars =“ simp:180” targ = enceladus.bi.def.3 km = 0.6 lat =(-71.5,90)lon =(220,360)'用python 3编写。
The current output is fixed at cam2map from=e119-b3331l1 to=rsmap-x map=enc.Ink.map pixres=mpp defaultrange=MAP res=300 minlat=-71.5 maxlat=90 minlon=220 maxlon=360 当前输出固定在cam2map上,从= e119-b3331l1到= rsmap-x map = enc.Ink.map pixres = mpp defaultrange = MAP res = 300 minlat = -71.5 maxlat = 90 minlon = 220 maxlon = 360
Currently, I have the code able to export a string of 0.6 for all of the iterations of lev1tolev2, but each one of these is going to be different. 目前,我的代码能够为lev1tolev2的所有迭代导出字符串0.6,但是每个迭代都会有所不同。
cam2map = Call("cam2map")
cam2map.kwargs["from"] = old_lev1tolev2.kwargs["from"]
cam2map.kwargs["to"] = "rsmap-x"
cam2map.kwargs["map"] = "enc.Ink.map"
cam2map.kwargs["pixres"] = "mpp"
cam2map.kwargs["defaultrange"] = "MAP"
**cam2map.kwargs["res"] = float((old_lev1tolev2.kwargs["km"]))**
cam2map.kwargs["minlat"] = lat[0]
cam2map.kwargs["maxlat"] = lat[1]
cam2map.kwargs["minlon"] = lon[0]
cam2map.kwargs["maxlon"] = lon[1]
I have two questions, why is this not converting the string to a float? 我有两个问题,为什么不将字符串转换为浮点数? And, why is this not iterating over all of the lev1tolev2 commands as everything else in the code does? 而且,为什么这不像代码中的其他所有命令一样遍历所有lev1tolev2命令?
The full code is available here. 完整代码可在此处获得。
it doesn't seem to clear to me, but from you syntax here : 对我来说似乎还不清楚,但是从您这里的语法:
**cam2map.kwargs["res"] = float((old_lev1tolev2.kwargs["km"]))**
I'd bet that cam2map.kwargs["res"] is a dict, and you thought that it would convert every values in the dict, using the ** syntax. 我敢打赌,cam2map.kwargs [“ res”]是一个字典,而您认为它将使用**语法转换字典中的每个值。 The float built-in should then be called in a loop over the elements of the dict, or possible a list-comprehension as here : 然后,应在dict的元素上循环调用内置的float,或者可能在此处进行列表理解:
cam2map.kwargs["res"] = dict()
for key, value in old_lev1tolev2.kwars["res"].items():
cam2map.kwargs["res"][key] = float(value)
Edit : Ok so, it seems you took the string 'from=e119-b3331l1 mappars="simp:180" targ=enceladus.bi.def.3 km=0.6 lat=(-71.5,90) lon=(220,360)' And then thought that calling youstring.kwargs would give you a dict, but it won't, you can probably parse it to a dict first, using some lib, or, you use mystring.split('=') and then work your way to a dict first, like that: 编辑:好的,看来您是从字符串'from = e119-b3331l1 mappars =“ simp:180” targ = enceladus.bi.def.3 km = 0.6 lat =(-71.5,90)lon =(220,360)'然后以为调用youstring.kwargs会给你一个dict,但是不会,你可以先使用一些lib将其解析为一个dict,或者使用mystring.split('=')然后再处理你的首先是一个命令,像这样:
output = dict()
for one_bit in lev_1_lev2.split(' '):
key, value = one_bit.split('=')
output[key] = value
The problem occurred at a different location in the code. 该问题发生在代码中的其他位置。 def escape_kw_value(value): def escape_kw_value(value):
if not isinstance(value, str):
return value
elif (value.startswith(('"', "'")) and value.endswith(('"', "'"))):
return value
# TODO escape the quote with \" or \'
#if value.startswith(('"', "'")) or value.endswith(('"', "'")):
# return value
if " " in value:
value = '"{}"'.format(value)
return value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.