【问题标题】:Added lines in csv file python - "wb" doesnt work在 csv 文件 python 中添加行 - “wb”不起作用
【发布时间】:2014-12-17 11:54:48
【问题描述】:

除了在 csv 文件中的代码之外,现在运行此代码后每个值之间都有一个空行 - 当我用谷歌搜索这个问题时,它建议使用“wb”,但返回错误

TypeError: 'str' does not support the buffer interface

键盘代码为2580,CSV格式为

bakerg,ict,George Baker,11HM,NORMAL

代码

from tkinter import *
import csv
import os

def upgradetoadmin():
    global masterpassword
    masterpassword = []
    def one():
        masterpassword.append("1")
        arraycheck()
    def two():
        masterpassword.append("2")
        arraycheck()
    def three():
        masterpassword.append("3")
        arraycheck()
    def four():
        masterpassword.append("4")
        arraycheck()
    def five():
        masterpassword.append("5")
        arraycheck()
    def six():
        masterpassword.append("6")
        arraycheck()
    def seven():
        masterpassword.append("7")
        arraycheck()
    def eight():
        masterpassword.append("8")
        arraycheck()
    def nine():
        masterpassword.append("9")
        arraycheck()
    def zero():
        masterpassword.append("0")
        arraycheck()
    def clear():
        global masterpassword
        masterpassword = []
    def blankremover():
        input = open('Student Data.csv', 'rb')
        output = open('Student Data2.csv', 'wb')
        writer = csv.writer(output)
        for row in csv.reader(input):
            if row:
                writer.writerow(row)
        input.close()
        output.close()
    def arraycheck():
        global masterpassword
        if len(masterpassword) == 4:
            if masterpassword == ['2','5','8','0']:
                print("Success")
                my_file = open('Student Data.csv', 'r')
                r = csv.reader(my_file)
                lines = [l for l in r]
                my_file.close()
                print(lines)
                i = 0
                for item in lines:
                    admininfy = whotomakeanadmin.get()
                    if item[0] == admininfy:
                        print(item)
                        print("YAY")
                        item[4] = "ADMIN"
                        print(item)
                        print(lines)
                        os.remove('Student Data.csv')
                        writer = csv.writer(open('Student Data.csv', 'w'))
                        writer.writerows(lines)
                        print(admininfy + " is now an admin")

            else:
                print("Invalid Code")
            masterpassword = []

    keypadwindow = Tk()
    keypadwindow.iconbitmap("hXYTZdJy.ico")
    keypadwindow.title("ADMIN UPGRADER")
    whotomakeanadmin = Entry(keypadwindow, width = 30)
    whotomakeanadmin.grid(column = 0, row = 0, columnspan = 3, pady = 10)
    Button(keypadwindow, text="1", height = 4, width = 10, command = one).grid(column = 0, row = 1)
    Button(keypadwindow, text="2", height = 4, width = 10, command = two).grid(column = 1, row = 1)
    Button(keypadwindow, text="3", height = 4, width = 10, command = three).grid(column = 2, row = 1)
    Button(keypadwindow, text="4", height = 4, width = 10, command = four).grid(column = 0, row = 2)
    Button(keypadwindow, text="5", height = 4, width = 10, command = five).grid(column = 1, row = 2)
    Button(keypadwindow, text="6", height = 4, width = 10, command = six).grid(column = 2, row = 2)
    Button(keypadwindow, text="7", height = 4, width = 10, command = seven).grid(column = 0, row = 3)
    Button(keypadwindow, text="8", height = 4, width = 10, command = eight).grid(column = 1, row = 3)
    Button(keypadwindow, text="9", height = 4, width = 10, command = nine).grid(column = 2, row = 3)
    Button(keypadwindow, text="0", height = 4, width = 10, command = zero).grid(column = 1, row = 4)
    Button(keypadwindow, text="CLEAR", height = 4, width = 10, command = clear).grid(column = 2, row = 4)
    keypadwindow.mainloop()

upgradetoadmin()

【问题讨论】:

  • 不确定它是否有效,但请尝试open('Student Data.csv', 'w', newline='') 并回复结果
  • io.UnsupportedOperation: 不可读
  • 不可读?你在哪里读书吗? (试试wba+
  • 谢谢,wb 和 newline='' 修复了它

标签: python csv python-3.x


【解决方案1】:

试试

 open('Student Data.csv', 'wb', newline='')

因为官方documentation要求我们这样做

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多