简体   繁体   English

从QRC资源文件创建熊猫数据框

[英]Create a pandas dataframe from a qrc resource file

I would like to save a CSV file into a qrc file and than read it putting its contents in a pandas dataframe, but I have some problems. 我想将CSV文件保存到qrc文件中,而不是将其内容放入pandas数据框中读取,但是我有一些问题。

I created a qrc file called res.qrc : 我创建了一个名为res.qrc的qrc文件:

<!DOCTYPE RCC><RCC version="1.0">
  <qresource>
    <file>dataset.csv</file>
  </qresource>
</RCC>

I compiled it obtaining the res_rc.py file. 我编译它以获得res_rc.py文件。

To read it I created a python script called resource.py : 为了阅读它,我创建了一个名为resource.py的python脚本:

import pandas as pd
import res_rc
from PySide.QtCore import *

file = QFile(":/dataset.csv")
df = pd.read_csv(file.fileName())
print(df)

But I obtain the error: IOError: File :/dataset.csv does not exist 但我IOError: File :/dataset.csv does not exist错误: IOError: File :/dataset.csv does not exist

All the files ( resource.py , res.qrs , res_rc.py , dataset.csv ) are in the same folder. 所有文件( resource.pyres.qrsres_rc.pydataset.csv )都位于同一文件夹中。

If I do res_rc.qt_resource_data I can see the contents. 如果执行res_rc.qt_resource_data ,则可以看到其中的内容。

How can I create the pandas dataframe? 如何创建熊猫数据框?

The qresource is a virtual path that only Qt knows how to obtain it and can change internally without warnings, in these cases what must be done is to read all the data and convert it into a stream with io.BytesIO qresource是一个虚拟路径,只有Qt知道如何获取它,并且可以在内部进行更改而不会发出警告,在这种情况下,必须做的是读取所有数据并将其转换为io.BytesIO的流。

import io
import pandas as pd
from PySide import QtCore
import res_rc


file = QtCore.QFile(":/dataset.csv")
if file.open(QtCore.QIODevice.ReadOnly):
    f = io.BytesIO(file.readAll().data())
    df = pd.read_csv(f)
    print(df)

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

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