[英]SOLVED - Python Pandas not importing .csv. Error: pandas.errors.EmptyDataError: No columns to parse from file
I am writing information into 2 .csv files (2 columns, separated by comma).我正在将信息写入 2 个 .csv 文件(2 列,以逗号分隔)。 I have ensured with time.sleep() that my desktop has enough time to write all the data to the file before pandas tries loading the information into the dataframe.我已经使用 time.sleep() 确保我的桌面有足够的时间在 pandas 尝试将信息加载到数据帧之前将所有数据写入文件。 It also seems like the issue remains with archorg.csv since I tried reversing the order for importing the file and pacman.csv didn't give an error, but archorg.csv still did.似乎问题仍然存在于archorg.csv,因为我尝试颠倒导入文件的顺序并且pacman.csv 没有给出错误,但archorg.csv 仍然存在。
onlinedf = pd.read_csv('/home/kia/Code/update/data/archorg.csv')
pacmandf = pd.read_csv('/home/kia/Code/update/data/pacman.csv')
When I try running this, I get the following error:当我尝试运行它时,我收到以下错误:
Traceback (most recent call last):
File "/home/kia/Code/update/main.py", line 28, in <module>
ugh = main()
File "/home/kia/Code/update/main.py", line 20, in __init__
filemgr.loadfiles()
File "/home/kia/Code/update/files.py", line 10, in loadfiles
onlinedf = pd.read_csv('/home/kia/Code/update/data/archorg.csv')
File "/usr/lib/python3.10/site-packages/pandas/util/_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "/usr/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 680, in read_csv
return _read(filepath_or_buffer, kwds)
File "/usr/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 575, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/usr/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 934, in __init__
self._engine = self._make_engine(f, self.engine)
File "/usr/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1236, in _make_engine
return mapping[engine](f, **self.options)
File "/usr/lib/python3.10/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 75, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 551, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file
Finally, I went to the interpreter and line by line entered the following:最后,我去解释器,逐行输入以下内容:
>>> import pandas as pd
>>> pd.read_csv('/home/kia/Code/update/data/archorg.csv')
package version
0 python-dulwich 0.20.45-1
1 sqlite-tcl 3.39.1-1
2 sqlite-doc 3.39.1-1
3 sqlite-analyzer 3.39.1-1
4 sqlite 3.39.1-1
.. ... ...
223 python-voluptuous 0.13.1-1
224 python-tldextract 3.3.1-1
225 perl-file-mimeinfo 0.33-1
226 perl-crypt-passwdmd5 1.42-1
227 perl-test-simple 1.302191-1
[228 rows x 2 columns]
It seems to get the job done with no issues.它似乎可以毫无问题地完成工作。 I've also posted a portion of the csv file below in case there's an issue there, although I have already checked it for extra commas/whitespaces/etc.我还在下面发布了 csv 文件的一部分,以防出现问题,尽管我已经检查了它是否有额外的逗号/空格/等。
package,version
python-dulwich,0.20.45-1
sqlite-tcl,3.39.1-1
sqlite-doc,3.39.1-1
sqlite-analyzer,3.39.1-1
sqlite,3.39.1-1
lemon,3.39.1-1
tp_smapi-lts,0.43-254
r8168-lts,8.050.03-9
acpi_call-lts,1.2.2-58
nvidia-lts,1:515.57-6
linux-lts-headers,5.15.55-1
linux-lts-docs,5.15.55-1
linux-lts,5.15.55-1
mattermost,7.1.1-1
node-gyp,9.1.0-1
trivy,0.30.0-1
sile,0.13.3-1
Edit: repo added for full review.编辑:添加回购以进行全面审查。
Edit 2: Got it to work using sep= instead of delim_whitespace, and then writing to the file with csv module instead of concatenation of strings, to remove any possibility of csv formatting errors.编辑 2:使用 sep= 而不是 delim_whitespace 使其工作,然后使用 csv 模块而不是字符串连接写入文件,以消除任何 csv 格式错误的可能性。 All files shown in repo for reference.存储库中显示的所有文件以供参考。
Your csv sample worked fine for me, the puzzling part is that your other file worked fine.您的 csv 样本对我来说效果很好,令人费解的是您的其他文件效果很好。 i would suggest you try a work around and i hope it works我建议您尝试解决方法,我希望它有效
import pandas as pd
df = pd.read_csv("filepath", delim_whitespace=True)
df[['Package', 'Version']] = df['package,version'].str.split(',', expand=True)
df.drop(columns = "package,version", inplace=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.