获取Python错误:指定的列太多

发布于 2025-02-10 01:23:13 字数 455 浏览 1 评论 0 原文

我正在通过Conda和我的航站楼运行Python。给了我一个脚本,应该能够在没有错误的情况下运行。该脚本导入URL并将其读为CSV。这就是我得到的:

url = 'https://www.aoml.noaa.gov/hrd/hurdat/hurdat2.html'
data, storm, stormList = readHURDAT2(url)
columnnames= ['a,b,c,etc']

错误以下一行开头:

for line in pd.read_csv(url, header=None, names=columnnames, chunksize=1):

计算机在输出此错误消息之前运行多个迭代:

Too many columns specified: expected 20 and found 1

I am running Python through conda and my terminal. I was given a script that should be able to run without error. The script imports a url and reads it as a csv. This is what I have been given:

url = 'https://www.aoml.noaa.gov/hrd/hurdat/hurdat2.html'
data, storm, stormList = readHURDAT2(url)
columnnames= ['a,b,c,etc']

The error begins with the next line:

for line in pd.read_csv(url, header=None, names=columnnames, chunksize=1):

The computer runs several iterations before outputting this error message:

Too many columns specified: expected 20 and found 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2025-02-17 01:23:13

之所以发生这种情况,是因为以HTML格式。我建议您将数据复制并粘贴到本地CSV文件中,并从中复制 read_csv 。另外,因为该文件具有特定格式分裂的格式文档标题行数据行,均具有不同数量的列,您需要设置 engine ='Python'读取它。最后,最多有21列,而不是20列。

代码应该看起来像这样:

for line in pd.read_csv('hurdat2.csv',   # <- Here
                        engine='python', # <- Here
                        header=None,
                        names=columnnames,
                        chunksize=1,
                        ):

This happens because the data in https://www.aoml.noaa.gov/hrd/hurdat/hurdat2.html is in HTML format. I'd recommend you copy and paste the data into a local CSV file and read_csv from it. Also, because the file has a specific format that splits the document into HEADER LINES and DATA LINES, both with a different number of columns, you'd need to set engine='python' to read it. Finally, there are the maximum of 21 columns, not 20.

The code should look something like this:

for line in pd.read_csv('hurdat2.csv',   # <- Here
                        engine='python', # <- Here
                        header=None,
                        names=columnnames,
                        chunksize=1,
                        ):
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文