简体   繁体   English

如何使用包含字符串的字典键以及包含列表的字典键创建csv文件?

[英]How to create csv file with dictionary key containing string as well dictionary key containing list?

name = {} 

name["one"] = "onestring"
name["two"] = "twostring"
name["three"] = [1, 2, 3, 4]
name["four"] = ["string", "hello"]

Expected output should be like

one            two           three      four

onestring    twostring        1         string
                              2         hello
                              3
                              4

how to create a csv file with above format?如何创建具有上述格式的csv文件? Dictionary values has some normal string values and some has list values.字典值有一些普通的字符串值,一些有列表值。 How to create a csv with above format??如何创建具有上述格式的 csv?

You can do this :你可以这样做 :

import csv

name = {'one': 'onestring', 'two': 'twostring', 'three': [1, 2, 3, 4], 'four': ['string', 'hello']}

# Be sure types are str or list only
assert(all(type(x) in (str, list) for x in name.values()))

csv_filename = "names.csv"

# Get length of the biggest list
max_length = 0
for key in name:
    value = name[key]
    if type(value) is list:
        list_length = len(value)
        if list_length > max_length:
            max_length = list_length


# Replace strings and lists by only lists, of equal length
equal_lengths_dict = dict()
for key in name:
    value = name[key]
    if type(value) is str:
        replacement_list = [""] * max_length
        replacement_list[0] = value
        equal_lengths_dict[key] = replacement_list
    elif type(value) is list:
        value_length = len(value)
        if value_length < max_length:
            number_empty_strings_to_add = max_length - value_length
            empty_strings_to_add = [""] * number_empty_strings_to_add
            replacement_list = value + empty_strings_to_add
            equal_lengths_dict[key] = replacement_list
        elif value_length == max_length:
            equal_lengths_dict[key] = value
        else:
            print("error")
    else:
        print("error")


# Get field names (header row of csv)
fieldnames = list(name.keys())

# Get values that will be on the same rows
csv_rows = list()
# Csv header
csv_rows.append(fieldnames)
# Other rows
list_of_values = equal_lengths_dict.values()
for index in range(max_length):
        one_row = list()
        for value in list_of_values:
            one_row.append(value[index])
        csv_rows.append(one_row)


with open(csv_filename, "w", newline="") as f:
    writer = csv.writer(f, delimiter="\t")
    writer.writerows(csv_rows)

You will have to add some checkings and tests.您将不得不添加一些检查和测试。

Perhaps you could rename the title of your question with someting like this : "Writing a dictionary to csv file with key/value pairs representing columns and with values of unequal length (strings and lists of variable lengths)".也许您可以使用以下内容重命名问题的标题:“将字典写入 csv 文件,其中键/值对表示列和不等长的值(字符串和可变长度列表)”。

A short solution (mostly from here Creating dataframe from a dictionary where entries have different lengths (duplicate?)):一个简短的解决方案(主要来自这里从字典中创建数据框,其中条目具有不同的长度(重复?)):

import pandas as pd

name = {}

name["one"] = "onestring"
name["two"] = "twostring"
name["three"] = [1, 2, 3, 4]
name["four"] = ["string", "hello"]

df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in name.items() ]))

df.to_csv("tmp.csv")

print(df)

output:输出:

         one        two  three    four
0  onestring  twostring      1  string
1        NaN        NaN      2   hello
2        NaN        NaN      3     NaN
3        NaN        NaN      4     NaN

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

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