pandas - 从 Zip Link Pandas 读取多个文件
问题描述
我有一个包含多个 csv 文件的 zip 文件 URL 链接。
如何直接在 Pandas 中单独读取文件,而不是在下载到本地文件夹后?
解决方案
也许试试这个作为一个例子......
from zipfile import ZipFile
from io import BytesIO
import urllib.request as urllib2
import pandas as pd
# get the zip file
url = urllib2.urlopen("http://www.example.com/aZipFileWithDifferentCSVFilesInIt.zip").read()
files = ZipFile(BytesIO(url))
# get the file names into a list; you could use this list for different things
# and to get certain files etc.
listOfFilesNames = files.namelist()
# open a file from the list; in this case position 23. This part could be a loop etc.
# and if 'specificFileName' do something different etc.
myCSV = files.open(listOfFilesNames[23])
# create the dataframe
df = pd.read_csv(myCSV)
# check the output
print(df)
推荐阅读
- javascript - 如何完成正则表达式语句以验证首字母缩写
- node.js - ExpressJS 使用没有 EJS 的数据重定向用户
- python - Tkinter ScrolledText 小部件滚动非常缓慢
- arduino - 即使连接了蓝牙,也可以使用按钮停止电机旋转
- reactjs - 使用 gh-pages 部署的 React App 未加载
- android - Android Sleeping Main Activity 与使用 websockets 的 Webview
- bash - 将文件从 S3 复制到 Ubuntu 18.04 中的临时文件夹的预定过程
- javascript - jQuery函数查找具有id的第一个元素
- python - 我应该在 Dockerfile 中设置什么 PYTHONPATH?
- python - 如何检查 Jupyter 在哪里寻找包