这是我的代码:
class Window(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400+30+30")
self.wButton = Button(self, text='Product Price List', command = self.OnButtonClick)
self.wButton.pack()
def OnButtonClick(self):
self.top = Toplevel()
self.top.title("Product Price List")
self.top.geometry("300x300+30+30")
self.top.transient(self)
self.wButton.config(state='disabled')
self.topButton = Button(self.top, text="Import Price list CSV", command = self.OnImport)
self.topButton.pack()
self.topButton = Button(self.top, text="Import Price Adjustment CSV", command = self.OnImport2)
self.topButton.pack()
self.topButton = Button(self.top, text="Import Price Adjustment CSV", command = self.OnImport3)
self.topButton.pack()
self.topButton = Button(self.top, text="Save As", command = self.OnSaveAs)
self.topButton.pack()
self.topButton = Button(self.top, text="CLOSE", command = self.OnChildClose)
self.topButton.pack()
def OnImport(self):
self.a = askopenfilename()
def OnImport2(self):
self.b = askopenfilename()
self.c = self.a.merge(self.b, how='left', left_on='Dynamic_spcMatrix', right_on='Dynamic_spcMatrix' )
def OnImport3(self):
self.d = askopenfilename()
self.d = self.d.dropna(axis=0)
self.g = self.d.groupby('Dynamic_spcMatrix')['Attribute_spcName'].apply(lambda x: ', '.join(x.astype(str))) #join attributes usin commas
self.c['Attribute_spcName'] = self.c['Dynamic_spcMatrix'].map(g)
self.c = self.c[['Type', 'Name', 'Currency_spcCode', 'Product_spcCfg_spcModel_spcId', 'Product_spcName', 'Attribute_spcName', 'Matrix_spcType', 'Start_spcDate', 'End_spcDate', 'Original_spcList_spcPrice', 'Max_spcSale_spcPrice', 'Min_spcSale_spcPrice', 'String_spcMatrix_spcColumn_spc1', 'String_spcMatrix_spcColumn_spc2', 'String_spcMatrix_spcColumn_spc3', 'String_spcMatrix_spcColumn_spc4','Number_spcMatrix_spcColumn_spc1']]
def OnSaveAs(self):
self.dlg = asksaveasfilename(confirmoverwrite=False)
self.fname = self.dlg
if self.fname != '':
f = open(self.fname, "a")
new_text = time.time()
f.write(str(new_text)+'\n')
f.close()
self.c.to_csv(self.fname, index=False)
def OnChildClose(self):
self.wButton.config(state='normal')
self.top.destroy()
if __name__ == "__main__":
window = Window(None)
window.title("Create Csv")
window.mainloop()
它在OnImport2
中给出以下错误:
AttributeError: 'unicode' object has no attribute 'merge'
我是python和所有其他语言的初学者。你能帮我解决这个问题吗?在
与
asksaveasfilename
一样,askopenfilename
返回一个字符串(当python2用于(可能)存储非ASCII文本时,python2调用unicode
)。如果您想从中读取(CSV数据),您必须显式地这样做,大概是通过您用于合并/输出的pandas
来实现的。在相关问题 更多 >
编程相关推荐