Python CSVWriter正在创建我的目录中似乎不存在的CSV文件 [英] Python CSVWriter is creating a CSV file that doesn't seem to exist in my directory

查看:384
本文介绍了Python CSVWriter正在创建我的目录中似乎不存在的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python写入CSV文件.问题是-看起来它正在写入内存中的某些隐藏对象,而从未输出到文件.

I'm trying to write to a CSV file using Python. Problem is - it looks like it's writing to some hidden object in memory and never outputting to a file.

这是我要使用的代码(在此处):

Here's the code I was trying to use (found here):

import csv

with open('employee_file.csv', mode='w') as employee_file:
    employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    employee_writer.writerow(['John Smith', 'Accounting', 'November'])
    employee_writer.writerow(['Erica Meyers', 'IT', 'March'])

执行此操作时,实际上没有文件输出(如Windows目录中所示),但是我也没有遇到任何错误.然后,如果我尝试以下操作:

When I do this, no file actually gets output (as shown in my Windows directory), but I also face no errors. Then, if I try something like the following:

with open('employee_file.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')

    for row in csv_reader:
        print(row)

它实际上输出我之前写过的内容(John SmithAccounting等).因此,似乎正在将其写入内存中的某个隐藏文件中.我发现此线程使用flush()close()实际写入磁盘,但是它们都不起作用.

It actually outputs what I had written prior (John Smith, Accounting, etc). So it seems like it's being written to some hidden file in memory. I found this thread which suggested using flush() and close() to actually write to disk, but neither of these did anything.

还选中了os.getcwd(),我位于正确的目录中.

Also checked os.getcwd() and I am in the correct directory.

还选中了employee_file.closed,它在with open部分中返回了False,之后又返回了True,这似乎可以正常工作.

Edit 2: Also checked employee_file.closed and it returns False in the with open section and True after, which seems to work as it should.

我尝试了另一件事,print(os.path.isfile([absolute path]))打印了True.因此,出于某种原因,在Python看来文件似乎100%存在.

Edit 3: One more thing I tried, print(os.path.isfile([absolute path])) printed True. So it seems like the file is 100% there in the eyes of Python for some reason.

:当我在python控制台中编写完全相同的代码时,它可以很好地工作并输出文件.可悲的是,我需要做一些修改,以致于我无法真正做到这一点.

Edit 4: When I write the exact same code in the python console, it works perfectly fine and outputs a file. Sadly, I have a bit too much modification I need to do that I can't really do this.

推荐答案

好吧,我找到了一种解决方法……虽然不是很干净的方法.

Well, I found a workaround... though not a very clean approach.

由于我发现它可以使用Python控制台很好地导出,因此我只是将数据处理封装在一个名为csvautomate.py的文件内的函数中(该函数返回行列表).然后我进入控制台并执行以下操作:

Since I discovered that it was exporting perfectly fine using the Python console, I just enclosed my data processing in a function (which returns a list of rows) inside of a file called csvautomate.py. Then I went into the console and did the following:

import csvautomate

with open("output.csv", mode='w', newline="") as f:
    writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    for row in csvautomate.processdata():
         writer.writerow(row)

它可以按预期工作,并输出正确格式的文件output.csv.

And it worked as intended, outputting a correctly formatted file called output.csv.

但是,这并不是一个理想的解决方案,因此我很好奇是否还有其他选择.似乎当我使用py file.py在cmd中运行文件时,由于某种原因,它无法按预期工作.当我使用64位python(我使用的是Win7 64位)时,出现此文件导出问题.当我使用32位python时,出现"python37.dll丢失"错误.

However this isn't really an ideal solution, so I'm curious if anyone has any other options. Seems when I run files in the cmd using py file.py it doesn't work as intended for some reason. When I use 64-bit python (I'm on Win7 64-bit) I get this file export problem. When I use 32-bit python I get a "python37.dll is missing" error.

通过选择新的安装路径(暂时在我的桌面上创建了一个文件夹)解决了32位python的"python37.dll丢失"错误.可能是先前位置的某种权限问题,但是,它不能解决不写入文件的问题.

Solved the 'python37.dll is missing" error with 32-bit python by choosing a new install path (just made a folder on my desktop for the time being). Looks like it was probably some kind of permissions issue with the previous location. However, it didn't fix the problem with not writing files.

这篇关于Python CSVWriter正在创建我的目录中似乎不存在的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆