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
如何创建具有上述格式的csv文件?字典值具有一些普通的字符串值,而某些具有列表值。如何使用上述格式创建csv?
答案 0 :(得分:0)
您可以这样做:
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)
您将不得不添加一些检查和测试。
也许您可以使用类似这样的方式来重命名问题的标题:“将字典/ csv文件写成具有代表列的键/值对并且长度不等长(字符串和可变长度列表)的csv文件。”
答案 1 :(得分:0)
一个简短的解决方案(主要是Creating dataframe from a dictionary where entries have different lengths(重复?)):
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)
输出:
one two three four
0 onestring twostring 1 string
1 NaN NaN 2 hello
2 NaN NaN 3 NaN
3 NaN NaN 4 NaN