我想将Python字典转储到具有特定自定义格式的JSON文件中.例如,以下字典my_dict
,
'text_lines': [{"line1"}, {"line2"}]
Run Code Online (Sandbox Code Playgroud)
甩了
f.write(json.dumps(my_dict, sort_keys=True, indent=2))
Run Code Online (Sandbox Code Playgroud)
看起来像这样
"text_lines": [
{
"line1"
},
{
"line2"
}
]
Run Code Online (Sandbox Code Playgroud)
虽然我更喜欢它看起来像这样
"text_lines":
[
{"line1"},
{"line2"}
]
Run Code Online (Sandbox Code Playgroud)
同样,我想要以下内容
"location": [
22,
-8
]
Run Code Online (Sandbox Code Playgroud)
看起来像这样
"location": [22, -8]
Run Code Online (Sandbox Code Playgroud)
(也就是说,它更像是一个坐标).
我知道这是一个美容问题,但对我来说保留这种格式以便于手动编辑文件非常重要.
有没有办法做这种定制?一个解释的例子会很棒(文档并没有让我走得太远).
我在SVG文件中有一个文本元素,我正在使用它生成lxml
.我想保留这个元素中的空格.我创建文本元素,然后尝试.set()
将xml:space
到preserve
,但没有我尝试似乎工作.我可能在概念上遗漏了一些东西.有任何想法吗?
我正在生成一个SVG文件,该文件旨在包含特定于Inkscape的标记.例如,inkscape:label
和inkscape:groupmode
.我使用lxml etree作为我的解析器/生成器.我想将label
和groupmode
标签添加到以下实例:
layer = etree.SubElement(svg_instance, 'g', id="layer-id")
Run Code Online (Sandbox Code Playgroud)
我的问题是如何实现这一点,以便在SVG中获得正确的输出形式,例如:
<g inkscape:groupmode="layer" id="layer-id" inkscape:label="layer-label">
Run Code Online (Sandbox Code Playgroud) 我想将“ pyparsing”解析结果作为字典显示出来,而无需进行后期处理。为此,我需要定义自己的密钥字符串。以下我能想到的最好的方法可以产生预期的结果。
要解析的行:
%ADD22C,0.35X*%
Run Code Online (Sandbox Code Playgroud)
码:
import pyparsing as pyp
floatnum = pyp.Regex(r'([\d\.]+)')
comma = pyp.Literal(',').suppress()
cmd_app_def = pyp.Literal('AD').setParseAction(pyp.replaceWith('aperture-definition'))
cmd_app_def_opt_circ = pyp.Group(pyp.Literal('C') +
comma).setParseAction(pyp.replaceWith('circle'))
circular_apperture = pyp.Group(cmd_app_def_opt_circ +
pyp.Group(pyp.Empty().setParseAction(pyp.replaceWith('diameter')) + floatnum) +
pyp.Literal('X').suppress())
<the grammar for the entire line>
Run Code Online (Sandbox Code Playgroud)
结果是:
['aperture-definition', '20', ['circle', ['diameter', '0.35']]]
Run Code Online (Sandbox Code Playgroud)
我认为这里的黑客是
pyp.Empty().setParseAction(pyp.replaceWith('diameter'))
Run Code Online (Sandbox Code Playgroud)
它始终匹配并且为空,但是随后我为其分配了所需的密钥名称。
这是最好的方法吗?我是否在滥用pyparsing来做本不该做的事情?
我有以下 PySide 应用程序,其预期功能是number_button
每 5 秒更新一次文本,一旦start_button
按下0 到 9 。
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.app_layout = QtGui.QVBoxLayout()
self.setLayout(self.app_layout)
self.setGeometry(300, 300, 50, 50)
self.count_to = 10
self.delay = 5000
self.timer = QtCore.QTimer(self)
self.timer.setSingleShot(True)
# start button
start_button = QtGui.QPushButton()
start_button.setText('START')
start_button.clicked.connect(self.startCount)
self.app_layout.addWidget(start_button)
# number button
self.number_button = QtGui.QPushButton()
self.number_button.setText('0')
self.app_layout.addWidget(self.number_button)
def startCount(self):
def updateButtonCount():
self.number_button.setText("%s" % count)
for count in range(0, self.count_to):
self.timer.singleShot(self.delay, updateButtonCount)
def main():
app = QtGui.QApplication(sys.argv) …
Run Code Online (Sandbox Code Playgroud)