如何将结果按安排的方式保存到csv?意思是添加额外的列解释什么“添加”、“删除”和“更改”
我尝试了diff.to_csv('diff.csv')并得到了这个错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'to_csv'
这是python代码
from csv_diff import load_csv, compare
diff = compare(
load_csv(open("list1.csv"), key="ean"),
load_csv(open("list2.csv"), key="ean")
)
diff.to_csv('diff.csv')
附加信息
list1.csv
price, oldprice,title,brand,category,unit,ean,,
17,,VR BOX Virtual Reality 3D Glasses Bluetooth Game Remote Control For Phone Iphone,other,3d glasses,2023700513,272434,,
18,,3d Glasses,other,3d glasses,1493500513,2272434,,
22,,Zefas Active 3D GlassesFor SmartPhones,zefas,3d glasses,1342700513,2272456,,
22.98,,3D Glasses Circular Polarized Lenses for Polarized TV| 3D Cinemas,other,3d glasses,1992100513,2272483,,
25,,max3 pro,other,3d glasses,1904600513,2272432,,
28.27,,Red Blue Clip on Anaglyph Glasses,other,3d glasses,2068900513,2272453,,
list2.csv
price,oldprice,title,brand,category,unit,ean,,
22.98,,3D Glasses Circular Polarized Lenses for Polarized TV| 3D Cinemas,other,3d glasses,1992100513,2272483,,
25,,max3 pro,other,3d glasses,1904600513,2272432,,
59,,Red-blue Cyan Anaglyph 3D,terratec,3d glasses,2103700513,2272428,,
65,,Sinogoodies Passive 3D GlassesFor SmartPhones,sinogoodies,3d glasses,1603700513,2272464,,
69.91,,G15-DLP 3D Active Shutter Glasses for DLP-LINK DLP LINK 3D for Optoma Sharp LG Acer BenQ Projectors,other,3d glasses,2039200513,227243,,
70,,Sinogoodies Passive 3D GlassesFor Multi,sinogoodies,3d glasses,1603600513,2272464,,
屏幕运行的结果
compare(
load_csv(open("list1.csv"), key="ean"),
load_csv(open("list2.csv"), key="ean")
)
是
{'added': [{'price': '59', 'oldprice': '', 'title': 'Red-blue Cyan Anaglyph 3D',
'brand': 'terratec', 'category': '3d glasses', 'unit': '2103700513', 'ean': '22
72428', '': ''}, {'price': '70', 'oldprice': '', 'title': 'Sinogoodies Passive 3
D GlassesFor Multi', 'brand': 'sinogoodies', 'category': '3d glasses', 'unit': '
1603600513', 'ean': '2272464', '': ''}, {'price': '69.91', 'oldprice': '', 'titl
e': 'G15-DLP 3D Active Shutter Glasses for DLP-LINK DLP LINK 3D for Optoma Sharp
LG Acer BenQ Projectors', 'brand': 'other', 'category': '3d glasses', 'unit': '
2039200513', 'ean': '227243', '': ''}], 'removed': [{'price': '17', 'oldprice':
'', 'title': 'VR BOX Virtual Reality 3D Glasses Bluetooth Game Remote Control Fo
r Phone Iphone', 'brand': 'other', 'category': '3d glasses', 'unit': '2023700513
', 'ean': '272434', '': ''}, {'price': '18', 'oldprice': '', 'title': '3d Glasse
s', 'brand': 'other', 'category': '3d glasses', 'unit': '1493500513', 'ean': '22
72434', '': ''}, {'price': '22', 'oldprice': '', 'title': 'Zefas Active 3D Glass
esFor SmartPhones', 'brand': 'zefas', 'category': '3d glasses', 'unit': '1342700
513', 'ean': '2272456', '': ''}, {'price': '28.27', 'oldprice': '', 'title': 'Re
d Blue Clip on Anaglyph Glasses', 'brand': 'other', 'category': '3d glasses', 'u
nit': '2068900513', 'ean': '2272453', '': ''}], 'changed': [], 'columns_added':
[], 'columns_removed': []}
此解决方案不能工作的
import pandas as pd
dataframe = pd.DataFrame(diff)
dataframe.to_csv("data.csv", header=True)
发布于 2020-05-09 22:53:18
其他解决方案无法工作,因为您的数据没有正确的DataFrame
结构。您需要首先访问顶级键中的dicts列表。
import pandas
df1 = pandas.DataFrame(diff['added'])
df1['change'] = 'added'
df2 = pandas.DataFrame(diff['removed'])
df2['change'] = 'removed'
df = df1.append(df2)
df.to_csv('diff.csv')
每个评论请求的更改
df1 = pandas.read_csv('list1.csv')
df1['version'] = 'list1'
df2 = pandas.read_csv('list2.csv')
df2['version'] = 'list2'
# keep only columns 'version', 'ean', 'price'
diff = df1.append(df2)[['version', 'ean', 'price']]
# keep only duplicated eans, which will only occur
# for eans in both original lists
diff = diff[diff['ean'].duplicated(keep=False)]
# perform a pivot https://pandas.pydata.org/pandas-docs/stable/user_guide/reshaping.html
diff = diff.pivot(index='ean', columns='version', values='price')
# back to a normal dataframe
diff = diff.reset_index()
diff.columns.name = None
# rename columns and keep only what we want
diff = diff.rename(columns={'list1': 'price1', 'list2': 'price2'})[['ean', 'price1', 'price2']]
diff['difference'] = diff['price2'] - diff['price1']
发布于 2020-05-09 22:11:32
你可以用熊猫包装安装熊猫
import pandas as pd
added_frame = pd.DataFrame(yourdict['added'])
removed_frame = pd.DataFrame(yourdict['removed'])
df = added_frame.append(removed_frame)
df.to_csv("data.csv", header=True)
发布于 2020-05-09 22:20:01
错误'dict' object has no attribute 'to_csv'
意味着您的diff
变量是一个字典,因此没有名为.to_csv
的方法。
您可以尝试将您的diff
强制进入csv文件,但它不能正常工作。您的diff
当前是一个字典,它的值是字典的数组。此格式不适用于csv。如果使用Pandas
包,则需要字典具有字符串数组的值。下面是一个例子的文档。
我建议您先考虑如何格式化diff
,然后再转换为csv。
使此工作的一个选择是从diff
字典中创建两个csvs。添加和删除的值实际上是csv的正确格式。下面是一个示例:
import csv
keys = diff['added'][0].keys()
with open('file_name.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(diff['added'])
这些是csv包的文档。
编辑:键应该从添加的第一个元素中获取键。
https://stackoverflow.com/questions/61704346
复制相似问题