简体   繁体   English

如何从没有引号且中间有换行符的字典中打印条目?

[英]How to print entries from a dictionary without quotes and with a newline in between?

Title is pretty self-explanatory, it's late in the day and I know I'll fell dumb from how easy it is;) Here is my code, I'm using DictReader .标题是不言自明的,现在已经很晚了,我知道我会因为它的简单性而变得愚蠢;) 这是我的代码,我正在使用DictReader

for line in myreader:
    if prop_code == line['Property Code']:
        print('Property Code: %s\n' % line['Property Code'],
              'Pivot ID: %s\n' % line['Pivot ID'],
              'Inwork File: %s\n' % line['Inwork File'],
              'Billing Manager E-mail: %s\n' % line['Billing Manager E-mail'],
              'Total Records: %s\n' % line['Total Records'],
              'Number of E-Bills: %s\n' % line['Number of E-Bills'],
              'Printed Records: %s\n' % line['Printed Records'],
              'File Date: %s\n' % line['File Date']) 

There are several ways to do this:做这件事有很多种方法:

Wrap each line inside a print将每一行都包裹在一个打印件中

print('Property Code: %s' % line['Property Code'])
print('Pivot ID: %s' % line['Pivot ID']')

Or you can use pprint或者你可以使用 pprint

from pprint import pprint
pprint(line)

Is this what you want?这是你想要的吗?

sample = {'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 39, 'height': 74}

for i in sample.keys():
    print '{} : {}\n'.format(i,sample.get(i))

Output:输出:

city : San Francisco

2 : Neato

name : Zed

1 : Wow

age : 39

height : 74

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM