我们怎样才能完成从字典中追加 excel 的过程?
How can we do the process of appending excel from dictionary?
我正在尝试编写一个代码来解析来自网络输出的一些数据,并在一组设备上循环并将结果附加到字典中,然后将每个字典键和值写入 excel sheet
我现在面临的问题是每次执行循环时键值都打印为列headers
dictionary = {"key1":[],"key2":[],"key3":[]}
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
dictionary_to_df("csv path,mode = "a",index = False, header = True)
输出是这样的:
key1
key2
key3
value1
value2
value3
key 1
key2
key3
value4
value5
value6
不过我想得到如下输出
key1
key2
key3
value1
value2
value3
value4
value5
value6
使用简单代码(pd.DataFrame.from_dict):
dictionary = {"key1":[],"key2":[]}
parse_value1=["value1","value2","value3"]
parse_value2=["value4","value5","value6"]
dictionary["key1"].extend(parse_value1)
dictionary["key2"].extend(parse_value2)
dictionary_to_df =pd.DataFrame.from_dict(dictionary)
dictionary_to_df.to_csv("test.csv",mode = "a",index = False, header = True)
您可以尝试将从循环生成的所有数据帧连接到一个更大的数据帧
dfs = []
for loop
dictionary = {"key1":[],"key2":[],"key3":[]}
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
dfs.append(dictionary_to_df)
df = pd.concat([dfs])
df.to_csv("csv path", mode = "a",index = False, header = True)
或者使 dictionary
对于 for-loop
是全局的
dictionary = {"key1":[],"key2":[],"key3":[]}
for loop
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
dictionary_to_df.to_csv("csv path", mode = "a",index = False, header = True)
或者检查文件头是否存在
for loop
dictionary = {"key1":[],"key2":[],"key3":[]}
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
with open("csv path", 'a') as f:
dictionary_to_df.to_csv(f,mode = "a",index=False, header=not f.tell())
我通过
解决了这个问题
- 在循环外定义header
- 将header写入
output.csv
文件
- 将字典转换为数据框
- 使用
header option = false
将数据框附加到 output.csv
文件中
我正在尝试编写一个代码来解析来自网络输出的一些数据,并在一组设备上循环并将结果附加到字典中,然后将每个字典键和值写入 excel sheet
我现在面临的问题是每次执行循环时键值都打印为列headers
dictionary = {"key1":[],"key2":[],"key3":[]}
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
dictionary_to_df("csv path,mode = "a",index = False, header = True)
输出是这样的:
key1 | key2 | key3 |
---|---|---|
value1 | value2 | value3 |
key 1 | key2 | key3 |
value4 | value5 | value6 |
不过我想得到如下输出
key1 | key2 | key3 |
---|---|---|
value1 | value2 | value3 |
value4 | value5 | value6 |
使用简单代码(pd.DataFrame.from_dict):
dictionary = {"key1":[],"key2":[]}
parse_value1=["value1","value2","value3"]
parse_value2=["value4","value5","value6"]
dictionary["key1"].extend(parse_value1)
dictionary["key2"].extend(parse_value2)
dictionary_to_df =pd.DataFrame.from_dict(dictionary)
dictionary_to_df.to_csv("test.csv",mode = "a",index = False, header = True)
您可以尝试将从循环生成的所有数据帧连接到一个更大的数据帧
dfs = []
for loop
dictionary = {"key1":[],"key2":[],"key3":[]}
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
dfs.append(dictionary_to_df)
df = pd.concat([dfs])
df.to_csv("csv path", mode = "a",index = False, header = True)
或者使 dictionary
对于 for-loop
dictionary = {"key1":[],"key2":[],"key3":[]}
for loop
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
dictionary_to_df.to_csv("csv path", mode = "a",index = False, header = True)
或者检查文件头是否存在
for loop
dictionary = {"key1":[],"key2":[],"key3":[]}
dictionary["key1"].append(parse_value1)
dictionary["key2"].append(parse_value2)
dictionary_to_df = pd.DataFrame(dictionary)
with open("csv path", 'a') as f:
dictionary_to_df.to_csv(f,mode = "a",index=False, header=not f.tell())
我通过
解决了这个问题- 在循环外定义header
- 将header写入
output.csv
文件 - 将字典转换为数据框
- 使用
header option = false
将数据框附加到
output.csv
文件中