CSV文件显示一个字符串输入

huangapple go评论79阅读模式
英文:

CSV File display a string input

问题

我已经创建了一个Tkinter应用程序来将数据存储在CSV文件中。以下是代码:

def add_file():
    global msg
    msg = entry.get()
    print(msg)
    if msg != '':
        listbox_tasks.insert(END, msg)
        entry.delete(0, END)
    else:
        messagebox.showinfo('Info', 'Please Fill In The Entry Box!')

def save_file(msg):
    with open('hello.csv', 'a', newline='') as f:
        writer = csv.writer(f, quotechar='"', quoting=csv.QUOTE_ALL)
        writer.writerow([msg])
    messagebox.showinfo('Info', 'Message Saved!')

scrollbar_tasks = Scrollbar(root)
scrollbar_tasks.pack(side=RIGHT, fill=Y)

listbox_tasks = Listbox(root, yscrollcommand=scrollbar_tasks.set, height=12, width=32, 
                         font=('Helvetica', 18))
listbox_tasks.pack(pady=20)

entry = Entry(label_frame1, font=('Helvetica', 18))
entry.grid(row=0, column=1)

my_create = Button(label_frame2, text='Add File', 
                   font=('Helvetica', 15), command=add_file)
my_create.grid(row=0, column=0, pady=10, padx=10)

my_entry = Button(label_frame2,  text='Save File', bg='green', fg='#ffffff',
                   font=('Helvetica', 15), command=lambda:save_file(msg))
my_entry.grid(row=1, column=0, pady=10, padx=10)

当我在输入框中输入"1,Apple"并点击保存文件按钮时,CSV文件中显示为"1,Apple"而不是1,"Apple"。

如何去除字符串中的引号?

英文:

I have created a Tkinter app to store the data in CSV file. The code as follow:

def add_file():
    global msg
    msg = entry.get()
    print(msg)
    if msg != '':
        listbox_tasks.insert(END, msg)
        entry.delete(0, END)
    else:
        messagebox.showinfo('Info', 'Please Fill In The Entry Box!')

def save_file(msg):
    with open('hello.csv', 'a', newline='') as f:
        writer = csv.writer(f, quotechar='"', quoting=csv.QUOTE_ALL)
        writer.writerow([msg])
    messagebox.showinfo('Info', 'Message Saved!')

scrollbar_tasks = Scrollbar(root)
scrollbar_tasks.pack(side=RIGHT, fill=Y)

listbox_tasks = Listbox(root, yscrollcommand=scrollbar_tasks.set, height=12, width=32, 
                         font=('Helvatica', 18))
listbox_tasks.pack(pady=20)

entry = Entry(label_frame1, font=('Helvatica', 18))
entry.grid(row=0, column=1)

my_create = Button(label_frame2, text='Add File', 
                   font=('Helvatica', 15), command=add_file)
my_create.grid(row=0, column=0, pady=10, padx=10)

my_entry = Button(label_frame2,  text='Save File', bg='green', fg='#ffffff',
                   font=('Helvatica', 15), command=lambda:save_file(msg))
my_entry.grid(row=1, column=0, pady=10, padx=10)

When I enter 1,Apple in entry and clicked save file button, in my CSV file display as "1,Apple" instead of 1,"Apple".

How could I strip out the string?

答案1

得分: 3

这个写入函数将十进制数据(未引用)和其他数据(已引用)拆分。

这是写入函数的代码:

decimals = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'}
def save_file(msg):
    with open('hello.csv', 'a', newline='') as f:
        msg += ","
        formatted = ''
        quote = False
        block = ''
        for i in msg:
            if i not in decimals:
                if i != ",":
                    quote = True
                    block += i
                else:
                    if quote:
                        block = '"' + block + '"'
                    formatted += block
                    formatted += ','
                    quote = False
                    block = ''
            else:
                block += i
        formatted = formatted[:-1]
        f.write(formatted + "\n")

    messagebox.showinfo('Info', 'Message Saved!')

当我使用以下代码时:

msg = "2,gerry,2.2,erderrer,42.0,22hellohelloh4llo,123.456789"
save_file(msg)

我在CSV文件中得到以下内容:

2,"gerry",2.2,"erderrer",42.0,"22helloh4llo",123.456789

该行以换行符结尾。

英文:

This writer function splits decimal data (not quoted) and other data (quoted).

This is the writer function:

decimals = {'0','1','2','3','4','5','6','7','8','9','.'}
def save_file(msg):
    with open('hello.csv', 'a', newline='') as f:
        msg += ","#ending char
        formatted = ''
        quote = False
        block = ''
        for i in msg:
            if i not in decimals:
                if i != ",":
                    quote = True
                    block += i
                else:
                    if quote:
                        block = '"'+block+'"'
                    formatted += block
                    formatted += ','
                    #new block
                    quote = False
                    block = ''
            else:
                block += i
        formatted = formatted[:-1]#remove ending char
        f.write(formatted+"\n")

    messagebox.showinfo('Info', 'Message Saved!')

When I use this:

msg = "2,gerry,2.2,erderrer,42.0,22hellohelloh4llo,123.456789"
save_file(msg)

I get this in the CSV file:

2,"gerry",2.2,"erderrer",42.0."22helloh4llo",123.456789

The line is ending with a newline character.

huangapple
  • 本文由 发表于 2023年5月26日 12:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定