我在为 tkinter 应用程序生成输出时遇到一些问题,我想用该应用程序允许用户输入地址并将其附加到现有的 csv 中。文件我当前遇到的问题是在导出期间地址中的每个字符都被分隔,因此不是 31A Kabul st,而是 ,3,1,A, ,K,a,b,u,l, ,s, t...我知道我以前遇到过这个问题,这可能是一个简单的修复,但我对 tkinter 很陌生,对 python 也很陌生。任何建议将不胜感激。科迪
from Tkinter import *
from tkFileDialog import *
import csv
"""GUI APPLICATION with Buttons"""
class Application(Frame):
def __init__(self, master):
"""Initialise the frame"""
Frame.__init__(self,master)
self.grid()
self.create_Main()
self.location_columns()
self.Column_box()
self.loader_button()
def create_Main(self):
"""Create 3 buttons that do nothing"""
self.button = Button(self, text="QUIT", fg="red", command=self.quit )
self.button.grid(row =1, column= 6,sticky = E)
self.label0 = Label(self, text= "Enter address")
self.label0.grid(row =1, column= 0, columnspan = 2, sticky = W)
def location_columns(self):
"""Location Columns"""
self.label = Label(self, text= "Flat letter or number")
self.label.grid(row =2, column= 0, sticky = W)
self.label1 = Label(self, text= "Street Number")
self.label1.grid(row =2, column= 1, sticky = W)
self.label2 = Label(self, text= "Street Name")
self.label2.grid(row =2, column= 2, sticky = W)
self.label3 = Label(self, text= "St suffix")
self.label3.grid(row =2, column= 3, sticky = W)
self.label4 = Label(self, text= "Suburb or Town")
self.label4.grid(row =2, column= 4, sticky = W)
self.label5 = Label(self, text= "City")
self.label5.grid(row =2, column= 5, sticky = W)
self.label6 = Label(self, text= "Region")
self.label6.grid(row =2, column= 6, sticky = W)
def Column_box(self):
"""Column Assoc Textboxes"""
self.textbox = Entry(self)
self.textbox.grid(row = 3, column = 0, sticky = W)
self.textbox1 = Entry(self)
self.textbox1.grid(row = 3, column = 1, sticky = W)
self.textbox2 = Entry(self)
self.textbox2.grid(row = 3, column = 2, sticky = W)
self.textbox3 = Entry(self)
self.textbox3.grid(row = 3, column = 3, sticky = W)
self.textbox4 = Entry(self)
self.textbox4.grid(row = 3, column = 4, sticky = W)
self.textbox5 = Entry(self)
self.textbox5.grid(row = 3, column = 5, sticky = W)
self.textbox6 = Entry(self)
self.textbox6.grid(row = 3, column = 6, sticky = W)
self.txt = Text(self, height=4, wrap = NONE)
self.txt.grid(row=5, columnspan=7, sticky="nsew")
self.txt.delete(0.0, END)
self.txt.configure( state="disabled")
self.submit_button = Button(self, text = "Add to current", command = self.reveal)
self.submit_button.grid(row = 4, column = 0, sticky = W)
self.flush_button = Button(self, text = "Add To File", command = self.Address)
self.flush_button.grid(row = 10, column = 0, sticky = W)
"""Scroll Bar"""
scrollb = Scrollbar(self.txt, command=self.txt.yview)
scrollb.pack( side = RIGHT, fill=Y)
self.txt['yscrollcommand'] = scrollb.set
def reveal(self):
"""prevents user from entering/editing the txt box and wrong format"""
self.txt.configure(state="normal")
content = self.textbox.get()
content1 = self.textbox1.get()
content2 = self.textbox2.get()
content3 = self.textbox3.get()
content4 = self.textbox4.get()
content5 = self.textbox5.get()
content6 = self.textbox6.get()
content = str(content.replace(" ",""))
content1 = str(content1.replace(" ", ""))
"""content2 = str(content2.replace(" ", ""))
#dont want to remove the space from roads like
# West Coast Road. etc..."""
content3 = str(content3.replace(" ", ""))
content4 = str(content4.replace(" ", ""))
content5 = str(content5.replace(" ", ""))
content6 = str(content6.replace(" ", ""))
"""Concatenate Flat and address fields"""
try:
content = int(content)
AddNum = str(content) +"/" +str(content1)
except ValueError:
AddNum = str(content1) + str.upper(content)
"""Add all values onto string"""
try:
count = len(AddNum) +len(content2) + len(content3) +len(content4) +len(content5) +len(content6)
if count == 0:
self.errorbox = Label(self, fg="red", text= "Please Enter an Address!")
self.errorbox.grid(row =1, column= 1, columnspan = 2, sticky = W)
raise Exception
else:
try:
self.errorbox.destroy()
message = "'"+ str(AddNum)+"'", "'"+ str(content2) +"'","'"+ str(content3)+"'" , "'"+str(content4) +"'", "'"+str(content5)+"'" , "'"+str(content6)+"'"
except:
message = "'"+ str(AddNum)+"'", "'"+ str(content2) +"'","'"+ str(content3)+"'" , "'"+str(content4) +"'", "'"+str(content5)+"'" , "'"+str(content6)+"'"
except ValueError:
try:
self.errorbox.destroy()
except:
raise Exception
try:
content1 = int(content1)
except ValueError:
self.errorbox = Label(self,fg="red", text= "Street Number must be an integer!")
self.errorbox.grid(row =1, column= 1, columnspan = 2, sticky = W)
raise Exception
"""Prevent interaction with txt box"""
self.txt.insert(0.0 ,'\n')
self.txt.insert(0.0 ,message)
self.txt.grid(row=5, columnspan = 7, sticky="nsew")
self.txt.configure( state="disabled")
def loader_button(self):
self.b_loader = Button(self, text = "Load File", command = self.loader)
self.b_loader.grid(row = 9, column = 0, sticky = W)
def loader(self):
self.filename = askopenfilename(parent=root)
self.textbox10 = Entry(self, width = 60)
self.textbox10.grid(row = 9, column = 1, columnspan = 3, sticky = W)
self.textbox10.insert(0,self.filename)
with open(self.filename, "r") as infile:
Address = infile.read()
self.contbox = Text(self, height=4, wrap = NONE)
self.contbox.grid(row=11, columnspan=7, sticky="nsew")
self.contbox.insert(0.0 ,Address)
self.contbox.configure( state="disabled")
scrolla = Scrollbar(self.contbox, command = self.contbox.yview)
scrolla.pack( side = RIGHT, fill=Y)
self.contbox['yscrollcommand'] = scrolla.set
def Address(self):
Address = self.txt.get(0.0, END)
list1= (Address)
print list1
if len(Address) <= 1:
raise Exception
else:
with open(self.filename, 'a') as csvfile:
listwriter = csv.writer(csvfile, delimiter=',')
print Address
listwriter.writerow(Address)
csvfile.close()
root = Tk()
root.title("AddressApp")
root.geometry("870x300")
app = Application(root)
root.mainloop()
root.destroy()
最佳答案
The issue that im having currently is during the export each character in the address is getting delimited so instead of 31A Kabul st, it would be ,3,1,A, ,K,a,b,u,l, ,s,t…
这几乎总是意味着您将单个字符串而不是字符串列表传递给 writerow
。请记住,字符串的作用类似于单字符字符串序列。
因此,您最有可能想要更改的是:
listwriter.writerow([Address])
但是,如果您只有一列,我不确定您为什么要使用 CSV。
更有可能的是,您真正想要做的是首先将 Address
制作为一个列表。代码中所有执行此类操作的位置:
message = "'"+ str(AddNum)+"'", "'"+ str(content2) +"'","'"+ str(content3)+"'" , "'"+str(content4) +"'", "'"+str(content5)+"'" , "'"+str(content6)+"'"
...您可能实际上只是想要这个:
message = [AddNum, content2, content3, content4, content5, content6]
...至少如果 message
是您最终要尝试写入 CSV 的行。
关于python - 从csv输出python中删除所有逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21322829/