gpt4 book ai didi

Python 在终端中使用 .csv 文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:30:47 24 4
gpt4 key购买 nike

我编写了以下脚本,在使用 pyCharm 时可以完美运行,但是当我在终端中运行它时,它会出现以下错误:

  File "/Users/Chris/PycharmProjects/firstfile/trial.py", line 6, in <module>
r = pf.read_csv('python.csv')
File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 562, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 315, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 645, in __init__
self._make_engine(self.engine)
File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 799, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 1213, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas/parser.pyx", line 358, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3427)
File "pandas/parser.pyx", line 628, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:6861)
IOError: File python.csv does not exist

有人能指出正确的方向吗?我猜这与 csv 文件不在正确的路径或目录中有关。现在我将 csv 文件保存在与我的 .py 项目相同的文件夹中。我还检查并确保安装了正确的软件包,所以我不认为是这样。

import csv
import pandas as pf

r = pf.read_csv('python.csv')
r.head()

print r.describe()
tradeDates = r['Trade Date'].unique()

r.name = 'Trade Date'
for trades in tradeDates:
outfilename = trades
printName = outfilename + ".csv"
print printName
r[r['Trade Date'] == trades].to_csv(printName, index=False)

最佳答案

当您运行 python/Users/Chris/PycharmProjects/firstfile/trial.py 时,python 在当前目录中查找 csv 文件,而不是在 /Users/Chris/PycharmProjects/firstfile。您要么需要在运行代码之前更改目录,要么需要像这样在 trial.py 中使用完整路径:

import csv
import pandas as pf

r = pf.read_csv('/Users/Chris/PycharmProjects/firstfile/python.csv')
r.head()

关于Python 在终端中使用 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com