【问题标题】:TypeError: argument 1 must have a "write" method when running programTypeError:参数1在运行程序时必须有一个“write”方法
【发布时间】:2017-09-21 19:03:54
【问题描述】:

我遇到了 typeError 问题:

def compare(datafile,logfile,index_data,index_log,index_temp):
    datafile_csv = csv.reader(datafile, delimiter = ',',quotechar = '"')
    logfile_csv = csv.reader(logfile,delimiter = ',', quotechar = '"')
    row_data = []
    row_log = []
    row_data = datafile_csv.__next__()
    row_log = logfile_csv.__next__()
    logfile_len = sum (1 for lines in logfile_csv)
------>>>>#this is the part of code that is giving me an issue
    **out_write = csv.writer('resultfile',quoting=csv.QUOTE_ALL)**
    #need to think about it
    while index_data != logfile_len:
            if row_data[index_data:] == row_log [index_log:]:
                    out_writer.writerow(row_log)
                    index_data += 1
                    index_log += 1
                    index_temp = index_log
            else :
                    while row_data [index_data:] != row_log [index_temp:]:
                            index_temp += 1
                            if row_data[index_data:] == row_log [index_log:]:
                                    out_writer.writerow(row_log)
                    index_temp = index_log
    gui.msgbox('This is the name of the newly generated log file :', out_writer)

我看了很多例子,看起来语法是正确的。

我犯了什么错误?

感谢和问候

达尼洛

【问题讨论】:

  • csv.writer 获取文件,而不是文件名。
  • 你需要一个实现write方法的对象,而字符串openfile显然没有。考虑使用open(...)

标签: python python-3.x typeerror


【解决方案1】:
with open('resultfile','w') as csvfile:
    out_write = csv.writer(csvfile, quoting=csv.QUOTE_ALL)

【讨论】:

  • 感谢您的信息,它确实有效!现在我遇到了另一个问题,但它与程序的结构有关(我对其进行了大修),我发布了一个新问题,stackoverflow.com/questions/43671719/…,如果你能看到我做错了什么,请告诉我......我想我我搞砸了 csv 库的使用。
猜你喜欢
  • 2015-09-25
  • 2017-03-03
  • 2019-03-15
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多