【问题标题】:python csv write to a new additional file without replace old filepython csv写入一个新的附加文件而不替换旧文件
【发布时间】:2017-06-30 18:45:30
【问题描述】:

我正在尝试编写 csv 文件并在每次用户使用它时创建一个新文件,而不是替换旧文件。而且,用户需要在开头输入他们的名字,所以我需要文件名是用户的名字。这是我的代码:

    direction = "/Users/Desktop/"
    cvs_file_name = str(self.entrySub.get()) ###'entrySub' is where users enter the name, and I use .get() to retrieve it.### 
    with open(direction, cvs_file_name+'_results.csv', 'w') as resultFile:
        resultFileWrite = csv.writer(resultFile, quoting=csv.QUOTE_ALL)
        resultFileWrite.writerow(['Subject', "Session"])
        resultFileWrite.writerow([self.entrySub.get(), self.entrySes.get()])
        resultFile.flush()

但是,有一个错误 “以 open(direction, cvs_file_name+'_results.csv', 'w') 作为结果文件: TypeError: 需要一个整数(类型为 str)”。

非常感谢您的帮助!

【问题讨论】:

    标签: python csv csv-write-stream


    【解决方案1】:

    open的第一个参数是文件的整个路径,包括目录。 试试

    with open(direction + cvs_file_name + '_results.csv', 'w') as resultFile:
        etc
    

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多