我有跟踪消息,如:
_str = ['exec(\'g.\'+option_val+\'=\'+option_val+\'()\')\n', ' File "<string>", line 1, in <module>\n', ' File "/home/nazariy/git/investment-tools/app/core/utilities/dbmonet.py", line 16, in __init__\n raise ValueError(\'Could not connect to the MonetDB database\')\n', 'ValueError: Could not connect to the MonetDB database\n']
我正在努力取代每一个\&#39;出现&#34;在Pycharm:
_str = [str_.replace("\'",'"') for str_ in _str]
以上不起作用,它因某种原因拆分每一个字符。但上面的确适用于jupyter qtconsole ......
我还尝试在Pycharm中转义转义字符:
_str = [str_.replace("\\'",'"') for str_ in _str]
也行不通......
任何想法?感谢
修改
以下是生成回溯的代码段:
import sys, traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
pretty_traceback = repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
#print("---- All the items ---")
#for i, x in enumerate(pretty_traceback): print(str(i) + ": "+(x))
print("------------------------- before pretty -----------------------")
pretty_traceback = [i.replace(r"\'", '"') for i in pretty_traceback]
print(pretty_traceback)
print("------------------------ pretty escape -------------------")
pretty_traceback = [i.replace(r"\n", "") for i in pretty_traceback]
print(pretty_traceback)
print("------------------------ pretty replace -------------------")
pretty_traceback = ["<p>"+_str+"</p>" for _str in pretty_traceback]
print(pretty_traceback)
答案 0 :(得分:2)
使用Regex
<强>实施例强>
import re
_str = ['exec(\'g.\'+option_val+\'=\'+option_val+\'()\')\n', ' File "<string>", line 1, in <module>\n', ' File "/home/nazariy/git/investment-tools/app/core/utilities/dbmonet.py", line 16, in __init__\n raise ValueError(\'Could not connect to the MonetDB database\')\n', 'ValueError: Could not connect to the MonetDB database\n']
_str = [re.sub(r"\'", '"', i) for i in _str]
print(_str)
<强>输出:强>
['exec("g."+option_val+"="+option_val+"()")\n', ' File "<string>", line 1, in <module>\n', ' File "/home/nazariy/git/investment-tools/app/core/utilities/dbmonet.py", line 16, in __init__\n raise ValueError("Could not connect to the MonetDB database")\n', 'ValueError: Could not connect to the MonetDB database\n']