简体   繁体   English

有字典的清单需要在python中写入csv文件

[英]Have a list of dicts need to write csv file in python

I need to ouput a list of dicts in python to a csv file. 我需要将python中的字典列表输出到csv文件中。 But I wanted to split them by filename. 但是我想按文件名拆分它们。 I have a list of dicts that looks like this: 我有一个看起来像这样的字典列表:

{'Filename': 'F:\\Desktop\\Metadata\informationtechnologies1.pdf',
  'DESCRIPTION': [u'This is a test sentence.'],
  'NAME': [u'This is a test name'],
  'PERIOD': [],
  'INFO2': [u'TEST1',
   u'TEST2',
   u'TEST3',
   u'TEST4',
   u'TEST5',],
  'INFO': [u'TEST6',
   u'TEST7',
   u'TEST8',
   u'TEST9',
   u'TEST10',
   u'TEST11',]},
 {'Filename': 'F:\\Desktop\informationtechnologies.pdf',

I need to print these exactly how it appears in a single column, with the results being in a new row. 我需要精确地打印它们在单列中的显示方式,并将结果显示在新行中。 Preferably a new line after each filename to separate all the results. 最好在每个文件名后加一行以分隔所有结果。 For example: 例如:

Filename: informationtechnologies1.pdf DESCRIPTION: This is a sentence INFO2: TEST2 TEST3 TEST4 TEST5

Filename:informationtechnologies.pdf

I have tried the following code but it places it each list into single columns(one column can have a list of 20 pieces of data in the info columns): 我尝试了以下代码,但是将每个列表都放在一列中(一列可以在info列中包含20条数据的列表):

df = DataFrame.from_dict(results)
df.to_csv("F:\Desktop/meta.csv",encoding='utf-8')

I need the data to be all iterated in column 1, so if there is a list within a dict I want each item in that list to print on a new line 我需要在第1列中对所有数据进行迭代,因此,如果字典中有一个列表,我希望该列表中的每个项目都在新行上打印

What you show is not a csv file but a plain text file. 您显示的不是csv文件,而是纯文本文件。

Assuming your requirements are: 假设您的要求是:

  • if a directory contains the key Filename print on first line the key, a colon, a space and the value 如果目录包含密钥Filename在第一行打印密钥,冒号,空格和值
  • for all other key, value items in the directory, first print a line with the key followed by a colon, then assuming the value is a list of unicode strings, print each value of the list encoded as utf8 on a line; 对于目录中的所有其他键,值项,首先在键上打印一行,后跟冒号,然后假定该值是unicode字符串列表,在一行上打印编码为utf8的列表的每个值; if the value is not a list, convert it to unicode and print it encoded as utf8 如果该值不是列表,则将其转换为unicode并打印为utf8
  • print an empty line after each directory 在每个目录后打印一个空行

I would use explicit loops and tests, because I do not know any tool that does this out of the box: 我将使用显式循环和测试,因为我不知道有什么工具可以立即使用:

with open("F:\\Desktop/meta.csv", "w") as fd:
    for d in results:
            if 'Filename' in d:
                dummy = fd.write('Filename: ' + d['Filename'] + '\n')
            for k, l in d.iteritems():
                if k != 'Filename':
                    dummy = fd.write(k + ':\n')
                    if isinstance(l, list):
                        for item in l:
                            dummy = fd.write(item.encode('utf8') + '\n')
                    else:
                        dummy = fd.write(unicode(l).encode('utf8') + '\n')
            dummy = fd.write('\n')

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

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