我有一个文件,如下所示:使用python,我想将这些多行代码转换为单行代码,如下所示。在这方面请帮帮我
x = {
"name": "Ken",
"age": 45,
"married": True,
"children": ("Alice", "Bob"),
"pets": [ 'Dog' ],
"cars": [
{"model": "Audi A1", "mpg": 15.1},
{"model": "Zeep Compass", "mpg": 18.1}
],
}
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
x.keys()
for key in x.keys():
print(key)
keys = key.strip().split("\n")
keys = list(key)
print(keys)为此,我得到了如下输出
['name']
['age']
['married']
['children']
['pets']
['cars']预期输出:
['name','age','married','children','pets','cars']发布于 2019-06-12 18:42:35
from fpdf import FPDF
x = {
"name": "Ken",
"age": 45,
"married": True,
"children": ("Alice", "Bob"),
"pets": [ 'Dog' ],
"cars": [
{"model": "Audi A1", "mpg": 15.1},
{"model": "Zeep Compass", "mpg": 18.1}
],
}
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
list_of_keys = [i for i in x.keys()]
print(list_of_keys)结果:
['name', ' age', 'married', 'children', 'pets', 'cars']https://stackoverflow.com/questions/56560156
复制相似问题