简体   繁体   English

python打开文本文件,pandas出现错误

[英]python open textfile wih pandas getting error

A simple code to open a text file with pandas module as published a lot here in the Forum一个简单的代码,用于打开带有 Pandas 模块的文本文件,如论坛中大量发布的那样

import pandas as pd
data = pd.read_csv('list.txt', sep=" ", header=None)
data.columns = ["a", "b", "c", "etc."]
print(data)

but I get an error message related to JSON.但我收到一条与 JSON 相关的错误消息。

Traceback (most recent call last): File "/home/robert/Python/MPC_data/comet_pandas.py", line 1, in import pandas as pd File "/usr/lib/python3.8/site-packages/pandas/ init .py", line 183, in v = get_versions() File "/usr/lib/python3.8/site-packages/pandas/_version.py", line 23, in get_versions return json.loads(version_json) AttributeError: module 'json' has no attribute 'loads'回溯(最近通话最后一个):文件“/home/robert/Python/MPC_data/comet_pandas.py”,1号线,在进口大熊猫为PD文件“/usr/lib/python3.8/site-packages/pandas/ INIT .py”,第 183 行,在 v = get_versions() 文件“/usr/lib/python3.8/site-packages/pandas/_version.py”,第 23 行,在 get_versions 中返回 json.loads(version_json) AttributeError: module 'json' 没有属性 'loads'

CJ95O010 1997 03 29.6644 0.913593 0.994932 130.6195 283.3707 88.9855 20200514 -2.0 4.0 C/1995 O1 (Hale-Bopp) MPC106342 PJ96R020 2019 02 11.5155 2.589984 0.313869 333.2955 40.0430 2.6004 20200514 11.5 4.0 P/1996 R2 (Lagerkvist) NK 1615 PJ97B010 2022 03 28.8307 2.054229 0.760061 183.3862 328.8921 12.3403 20200514 15.0 2.0 P/1997 B1 (Kobayashi) MPC 30063 PJ98V24S 2018 01 18.0487 3.435537 0.241739 244.7371 159.0301 5.0243 20200514 13.0 2.0 P/1998 VS24 (LINEAR) MPC 75703 PJ99R28O 2019 06 26.0365 1.124239 0.671921 231.2769 137.8823 7.5658 20200514 20.0 2.0 P/1999 RO28 (LONEOS) NK 731 CJ95O010 1997 03 29.6644 0.913593 0.994932 130.6195 283.3707 88.9855 20200514 -2.0 4.0 C / 1995 O1(海尔 - 波普)MPC106342 PJ96R020 2019 02 11.5155 2.589984 0.313869 333.2955 40.0430 2.6004 20200514 11.5 4.0 P / 1996 R2(Lagerkvist)NK 1615 PJ97B010 2022 03 28.8307 2.054229 0.760061 183.3862 328.8921 12.3403 20200514 15.0 2.0 P / 1997 B1(小林)MPC 30063个PJ98V24S 2018 01 18.0487 3.435537 0.241739 244.7371 159.0301 5.0243 20200514 13.0 2.0 P / 1998 VS24(LINEAR)MPC 75703 PJ99R28O 2019 06 26.0365 1.124239 0.671921 231.2769 137.8823 7.5658 20200514 20.0 2.0 P / 1999 RO28 (LONEOS) NK 731

Any idea?任何的想法?

There is a problem with importing Pandas package in line 1. So please try to uninstall package: pip uninstall pandas第1行导入Pandas包有问题,请尝试卸载包: pip uninstall pandas

Then reinstall and run the code.然后重新安装并运行代码。 pip install pandas

It works有用

在此处输入图片说明

I deleted data.columns = ["a", "b", "c"] and sep = " "我删除了data.columns = ["a", "b", "c"] and sep = " "

It was the formatting of the text file这是文本文件的格式

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

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