在 Jupyter 上编码时在哪里上传 csv 然后读取它

Where to upload a csv to then read it when coding on Jupyter

我需要在互联网上的某个地方上传一些 CSV 文件,以便稍后使用 read_csv 在 Jupyter 中使用它。 有哪些简单的方法可以做到这一点?

CSV 包含一个数据库。我想将它上传到某个地方并使用 read_csv 在 Jupyter 中使用它,以便其他人在我向他们发送我的文件时可以 运行 代码。

您可以使用任何云存储提供商,例如 Dropbox 或 Google Drive。或者,您可以使用 Github。

要在笔记本中执行此操作,请像通常导入本地文件一样导入 pandas 和 read_csv。

import pandas as pd

url="https://raw.githubusercontent.com/cs109/2014_data/master/countries.csv"
c=pd.read_csv(url)

The CSV contains a database.

由于 CSV 包含一个数据库,我不建议像 Steven K in the . It would be a better option to upload it to either Google Drive or Dropbox as rightly said in the 提到的那样将其上传到 Github。

要从 Google 驱动器读取文件,您可以尝试以下操作:

  • 将文件上传到 Google 驱动器并单击 "Get Sharable Link" 和 确保拥有 link 的任何人都可以访问它。
  • 单击复制 link 并获取与 CSV 关联的文件 ID。 例如:如果这是 URL https://drive.google.com/file/d/108ARMaD-pUJRmT9wbXfavr2wM0Op78mX/view?usp=sharing 那么 108ARMaD-pUJRmT9wbXfavr2wM0Op78mX 就是文件 ID。

只需在下面的示例代码中使用文件ID

import pandas as pd

gdrive_file_id = '108ARMaD-pUJRmT9wbXfavr2wM0Op78mX'
data = pd.read_csv(f'https://docs.google.com/uc?id={gdrive_file_id}&export=download', encoding='ISO-8859-1')

在这里,您将向任何有权访问 link 的人开放 CSV。更好和更可控的方法是与已知的人共享访问权限,并使用像 PyDrive 这样的库,它是 Google API 的官方 Python 客户端的包装器。

注意: 因为你的问题没有提到你正在使用的 Python 版本,我假设 Python 3.6+ 并使用 f-strings in line #3 of the code. If you use any version of Python before 3.6, you would have to use format method 替换字符串中变量的值