Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有一个更干净、更有条理的组织;蟒蛇的;这些关于一个城市的印刷声明的方式?_Python - Fatal编程技术网

Python 是否有一个更干净、更有条理的组织;蟒蛇的;这些关于一个城市的印刷声明的方式?

Python 是否有一个更干净、更有条理的组织;蟒蛇的;这些关于一个城市的印刷声明的方式?,python,Python,这里我有一本字典,我省略了它们,但我试图让每个城市在用户在控制台中键入正确的名称时,以正确的对齐方式打印出以下内容。到目前为止,我刚刚使用了很多\t,这很好,但看起来相当混乱,也许有更好的方法来使用\t或其他东西。这是我的密码: mydict = { 'Shenzhen' : { 'Population': 14383936, 'Category': 'want to visit', 'Interesting Fact': 'was one of the fastest-

这里我有一本字典,我省略了它们,但我试图让每个城市在用户在控制台中键入正确的名称时,以正确的对齐方式打印出以下内容。到目前为止,我刚刚使用了很多
\t
,这很好,但看起来相当混乱,也许有更好的方法来使用
\t
或其他东西。这是我的密码:

mydict = { 'Shenzhen' : {
    'Population': 14383936,
    'Category': 'want to visit',
    'Interesting Fact': 'was one of the fastest-growing cities in the world during the 1990s and the 2000s',
    'Geographic Coordinates': (22.542883, 114.062996)
            } }


if userInput == 'Shenzhen':
    print( '{0}:\t\t\t\t\t{1}'.format("City Name","Shenzhen") )
    print( "{0}:\t\t\t\t\t{1}".format( mydict['Shenzhen'].keys()[0], mydict['Shenzhen'].values()[0] ) )
    print( "{0}:\t\t\t{1}".format( mydict['Shenzhen'].keys()[1], mydict['Shenzhen'].values()[1] ) )
    print("{0}:\t\t\t\t\t{1}".format("Longitude", mydict['Shenzhen']['Geographic Coordinates'][0]))
    print("{0}:\t\t\t\t\t{1}".format("Latitude", mydict['Shenzhen']['Geographic Coordinates'][1]))
    print( "{0}:\t\t\t\t\t{1}".format( mydict['Shenzhen'].keys()[3], mydict['Shenzhen'].values()[3] ) )
我希望它能像这样均匀地打印出来:

城市名称:深圳
经度:2138530259
纬度:2302968049
类别:想参观

等等。

如果您不喜欢代码中重复出现
\t
,可以使用以下事实:

>>> '\t' * 4 == '\t\t\t\t'
True
对于整数或浮点的一致对齐,可以使用以下语法:

>>> print('{:5d}'.format(100))
  100
在这里,您可以指示要插入到字符串中的整数/浮点的字符数。由于
100
仅为3个字符,因此将以2个前导空格显示


您可以阅读有关格式化打印语句的更多信息。

避免潮湿,请参阅。例如,您可以创建一个函数,对一个城市进行打印,然后重复调用它。谢谢!我想我只需要使用\t*4方法。您认为多个打印语句的样式不好吗?@FooFighter您试图做的基本上是将一个表打印到控制台。当我需要做类似的事情时,我使用了一个外部模块-。不确定是否有一种方法可以使表格打印代码看起来“漂亮”,而不用使用类似制表或自己编写一个简单的表格打印模块。