如何在字典中正确显示长字符串?

问题描述 投票:0回答:2

请参阅下面的字符串。我正在使用lmfit进行拟合,然后使用yaml将结果保存到字典中。我正在获取lmfit结果生成的以下字符串:

"[[Fit Statistics]]\n    # fitting method   = leastsq\n    # function\
    \ evals   = 4\n    # data points      = 70\n    # variables        = 2\n    chi-square\
    \         = 5.5122e+09\n    reduced chi-square = 81062428.0\n    Akaike info crit\
    \   = 1276.72198\n    Bayesian info crit = 1281.21897\n[[Variables]]\n    Amplitude:\
    \       12.6121224 +/- 0.24761617 (1.96%) (init = 12.61212)\n    Radius:     \
    \     41.0677874 +/- 0.11681467 (0.28%) (init = 41.06779)\n    Polydispersity:\
    \  0.05 (fixed)\n    Background:      1e-05 (fixed)\n    Roughness:       0 (fixed)\n\
    [[Correlations]] (unreported correlations are < 0.100)\n    C(Amplitude, Radius)\
    \ = -0.446" 

将其精美呈现的最佳方法是什么。 (例如:\n似乎不起作用)

python string dictionary pyyaml
2个回答
1
投票

有一个名为pprint的程序包。如果我没记错的话,它应该是Python标准库的一部分。以下是文档的链接。https://docs.python.org/3/library/pprint.html

快乐编码!保持安全!


0
投票

[如果您只是想以自己的观察来“呈现”这个美感,请尝试:

ls=string.split("\n") print(ls)

如果要从字符串中实际解析或将其用于其他用途,而不是将生成的列表转换为元组或字典。

© www.soinside.com 2019 - 2024. All rights reserved.