我正在尝试从 URL 将数据导入 jupyter 笔记本,但出现如下语法错误

问题描述 投票:0回答:1

从 pyodide.http 导入 pyfetch

异步下载(https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85数据,“auto.csv”): 响应=等待pyfetch(https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85数据) 如果响应.status == 200: 将 open("auto.csv", "wb") 作为 f: f.write(等待响应.bytes()) 当我在 jupyter 笔记本中运行此代码时,出现以下错误: 单元格 In[28],第 3 行 异步def下载(https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85数据,“auto.csv”): ^ 语法错误:语法无效

谁能告诉我原因是什么以及如何纠正? 谢谢

除了导入 Panda 和 Numpy 库之外,我还安装了“pip install pyopenssl”,但我不断收到语法错误。

url import jupyter
1个回答
0
投票

正确的链接位于这里

是:

https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data

我不确定你的 pyfetch 代码是基于什么的,因为它是不可能阅读你共享它的方式。

很好的例子,你的网址甚至不正确,你没有检查,而是按原样保留了帖子

改编后的代码从这里尝试将其应用到您的代码是:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="default" />
    <meta name="theme-color" content="#0072b5" />
    <meta name="name" content="CSV Getting Demo adapted from https://stackoverflow.com/a/76148659/8508004" />

    <title>CSV Getting Demo with ML Data</title>
    <link rel="icon" type="image/x-icon" href="./favicon.png" />
    <link
        rel="stylesheet"
        href="https://pyscript.net/latest/pyscript.css"
    />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    <link rel="stylesheet" href="https://pyscript.net/examples/assets/css/examples.css" />
</head>
<body>
    <div id="pandas-output" hidden>
        <h3>Output</h3>
        <div id="pandas-output-inner"></div>
    </div>
    <py-tutor>
            <py-config>
packages = [
    "numpy",
    "pandas",
    "jinja2"
]
[[fetch]]
from = 'https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data'
</py-config>

<py-script>
import pandas as pd

df = pd.read_csv('imports-85.data')
Element("pandas-output").element.style.display = "block"
display (df.head().style.format(precision=2), target="pandas-output-inner", append="False")
</py-script>
        </py-tutor>
    </section>
</body>
</html>

它不起作用,但我没有在一台合理的计算机上检查原因。这可能与此处描述的 CORS 问题类似。

最简单的事情是从与 pyfetch 一起使用的 CSV 开始,然后您就可以消除这个问题。示例 therethere 可以实现我知道的

turtles.csv
的效果。后一个你甚至可以通过在 Stackoverflow 中运行截图来查看工作。

© www.soinside.com 2019 - 2024. All rights reserved.