简体   繁体   English

Python不创建文件(csv或txt)

[英]Python not creating a file(csv or txt)

I am using windows 10 and Python 3.6.5 through Anaconda.我正在通过 Anaconda 使用 Windows 10 和 Python 3.6.5。

I am trying to create a CSV and then add a list to said CSV but I cannot get it to create the CSV in the first place.我正在尝试创建一个 CSV,然后向所述 CSV 添加一个列表,但我无法首先使用它来创建 CSV。 No error codes, just nothing.没有错误代码,什么都没有。 I have disabled all antivirus protection to make sure that wasnt the issue.我已禁用所有防病毒保护,以确保这不是问题。 I have tried on two different PCs.我在两台不同的电脑上试过。 I am sure it is something stupid as I am newish to python, but I cant find the answer.我确信这很愚蠢,因为我对 python 不熟悉,但我找不到答案。

I have tried two ways.我尝试了两种方法。

open("new.txt","w").close

and

import csv导入 csv

with open('names.csv', "w") as csv_file:
    csv_writer = csv.writer(csv_file, delimiter=",")

    print(csv_writer)

this gives me the memory position of csv_writer as expected but other than that does nothing.这按预期为我提供了 csv_writer 的内存位置,但除此之外什么都不做。

Please help a newbie out!请帮助一个新手! Thanks.谢谢。 I promise I have googled and tried everything I know.我保证我已经谷歌搜索并尝试了我所知道的一切。

I have tried your code and it works perfectly fine for me.我试过你的代码,它对我来说非常好。 Perhaps try the following to see exactly where you created the file:也许尝试以下操作以查看您创建文件的确切位置:

import csv, os

current_dir = os.getcwd()

with open('names.csv', "w") as csv_file:
    print('opened csv at {}/names.csv'.format(current_dir))
    csv_writer = csv.writer(csv_file, delimiter=",")
    print(csv_writer)

If you're using Anaconda Jupyter notebook works really well.如果您使用的是 Anaconda Jupyter 笔记本,则效果非常好。 But this can also be implemented anywhere.但这也可以在任何地方实施。 Make a new folder.新建一个文件夹。 Put the text file you want to open and the Python file in the same folder.将要打开的文本文件和 Python 文件放在同一文件夹中。 Do 'import pandas as pd' in your python file.在你的python文件中“导入pandas作为pd”。 Then do:然后做:

file = pd.read_csv('your_file').

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM