Python 如何去掉json字符串中的引号,这是由于将科学符号转换为精确值而产生的

Python 如何去掉json字符串中的引号,这是由于将科学符号转换为精确值而产生的,python,sql,json,Python,Sql,Json,我有一个字典项的列表,我将通过迭代更新到字典中。该字典将被转换为json字符串,该字符串将被插入到SQL数据库中。我的问题是,字典项中的某些值可能非常小(或很大),因此Python会自动将其放入科学符号中 例如,我有一个字典:{“Name”:{“Key1”:“0.000006”,“Key2”:“0.0000000001”}。我已经能够使用我在下面创建的函数将科学符号转换为精确的值字符串。但是现在我必须把它作为一个字符串,因为只要我在这个字符串上投射float(),它就会回到科学记数法。因此,当我

我有一个字典项的列表,我将通过迭代更新到字典中。该字典将被转换为json字符串,该字符串将被插入到SQL数据库中。我的问题是,字典项中的某些值可能非常小(或很大),因此Python会自动将其放入科学符号中

例如,我有一个字典:
{“Name”:{“Key1”:“0.000006”,“Key2”:“0.0000000001”}
。我已经能够使用我在下面创建的函数将科学符号转换为精确的值字符串。但是现在我必须把它作为一个字符串,因为只要我在这个字符串上投射
float()
,它就会回到科学记数法。因此,当我使用
json.dumps()
将字典放入json字符串中时,它会在这些字符串铸造的浮点值周围创建不必要的引号:
“{”Name:{”Key1:“0.000006”,“Key2:“0.000000000 1”}”
。我怎样才能去掉那些引语呢

def format_float(self, value):
    value_string = str(value)
    # Anything smaller than or equal to 1e-5 will get converted to scientific notation
    # If the value is in scientific notation
    if 1e-323 <= abs(value) <= 1e-5:
        # Split the string by the exponent sign
        split_value_string = value_string.split("e-")
        # Get the exponential
        exponential = int(split_value_string[1])
        # If there is a decimal, there could be digits after the decimal
        if "." in split_value_string[0]:
            precision = len(split_value_string[0].split(".")[1])
        else:
            precision = 0
        f = "%.*f" % (exponential + precision, value)
    elif value == float("inf") or value == float("-inf"):
        return value
    # Not in scientific notation
    else:
        if "." in value_string:
            precision = len(value_string.split(".")[1])
        else:
            precision = 0
        f = "%.*f" % (precision, value)
    # No decimal
    if "." not in f:
        f += ".0"

    p = f.partition(".")

    s = "".join((p[0], p[1], p[2][0], p[2][1:].rstrip("0")))  # Get rid of the excess 0s

    return s
def格式\u浮点(自身,值):
值\字符串=str(值)
#任何小于或等于1e-5的值都将转换为科学符号
#如果值是用科学符号表示的
如果1e-323
float()
不应该在科学记数法中添加任何内容。只是当python打印一个非常长的
浮点值时,它会用科学的符号来打印它。尝试使用
json.dumps()
,同时将值保持为
float
,它将为您提供所需的内容

编辑:让我重新措辞
json.dumps()
将为您提供有效的json。json规范允许将浮点数作为科学表示法给出,因此它在科学表示法中的事实不应该是一个问题。也就是说,如果加载已转储的内容,它将为您提供一个
浮点值
,您可以按照预期对其进行计算。e、 g.:
type(json.loads(json.dumps({'lol':1e-8}))['lol'])
给我
float

如果您确实需要json中没有科学符号,例如,如果读取json的东西不符合标准,那么您可以使用
json.dumps()
将其作为字符串转储,然后使用
re.sub()
替换数字

precision = 10
re.sub('\d+e-?\d+',lambda x: '%.*f'%(precision,float(x.group())),json.dumps({'lol':1e-8}))
给我

'{"lol": 0.0000000100}'

@OP说他们已经试过了。当
json.dumps
将所有内容转换为字符串时,它会将“有效”浮点数转换为科学表示法,就像您只需键入数字时REPL所做的那样。@phsyron您能对此进行扩展吗?如果我尝试类似于
print(json.dumps({“lol”:1e-8}))
,它会输出未转换的
{“lol”:1e-08}
。@physyron谢谢,这对我来说已经足够好了。我原本想用re.sub替换float,但字典中的其他键也可以是float,这将导致不必要的更改。我应该把它放在我的帖子里。我没想过把科学符号放进去,然后转换+1并接受。顺便说一句,您的正则表达式不考虑小数,因此
{'lol':1.5e-8}
将给我
{“lol”:1.0.0000000 500}
。改用
(\d*\)?\d+(e[+-]?\d+)
:)@你用json做什么不接受科学记数法?“只要我在这个字符串上加上float(),它就会回到科学记数法”-我想这里有一个误解。这不是
float()
所做的。你能创建一个简短的、独立的程序来演示
float()
如何使它回到科学记数法吗?@Rob类似这样的
print(float(“0.0000000000000000001”)
。我得到
1e-23