[英]Unable to create a dataframe from a list of dictionaries returned by a RPyC function
I am trying to use a function within a RPyC
threaded server which returns the dict
containing file attributes such as location, filename by looping over all the folders within the specified path.我正在尝试在
RPyC
线程服务器中使用一个函数,该函数通过循环遍历指定路径中的所有文件夹来返回包含文件属性(例如位置、文件名)的dict
。
However, when this is returned back to client, the list object (fl) is of type但是,当这返回给客户端时,列表对象 (fl) 的类型为
<netref class 'rpyc.core.netref.builtins.list'>
which I try to convert to a list using我尝试将其转换为列表
ft= list(ft)
but this too converts it to ' <class 'list'>
' and not 'list'
as expected.但这也将其转换为 '
<class 'list'>
' 而不是'list'
正如预期的那样。
I'd like this to be converted to a dataframe but using ' df = pd.DataFrame(fl)
' returns an error 'AttributeError: cannot access 'keys'
我希望将其转换为数据帧,但使用 '
df = pd.DataFrame(fl)
' 返回错误'AttributeError: cannot access 'keys'
RPyC server function: RPyC 服务器功能:
The closest I came to finding a related response was in this post but I still don't know if I understand this right.我最接近找到相关回复的是在这篇文章中,但我仍然不知道我是否理解这一点。 Is there a way to put this into a dataframe or convert to normal list which can then be converted easily?
有没有办法将其放入数据帧或转换为可以轻松转换的普通列表? Any help is appreciated.
任何帮助表示赞赏。
Server:服务器:
PATH = r"C:\Temp"
def exposed_fquery():
fl = []
for (dpath, dname, fname) in os.walk(PATH):
for f in fname:
td = {}
td['Location'] = dpath
td['Name'] = f
fl.append(td)
print (fl)
return (fl)
Client:客户:
con = rpyc.connect('localhost',5000)
s = con.root.Server()
filemap = s.fquery(index)
print (type(filemap), "\n", filemap)
print (type(ft), "\n",ft)
ft= list(ft)
print (type(ft))
Result:结果:
<netref class 'rpyc.core.netref.builtins.list'>
[{'Location': 'C:\\Temp\\', 'Name': 'file1.txt'}, {'Location': 'C:\\Temp\\', 'Name': 'Test.txt'}]
<class 'list'>
I'm not sure where exactly your troubles are coming from, but I suspect you've encountered some bad input because with the example you provided it seems to work.我不确定您的麻烦究竟来自哪里,但我怀疑您遇到了一些错误的输入,因为您提供的示例似乎有效。
In [7]: ft = [{'Location': 'C:\\Temp\\', 'Name': 'file1.txt'}, {'Location': 'C:\\Te
...: mp\\', 'Name': 'Test.txt'}]
In [8]: pd.DataFrame(ft)
Out[8]:
Location Name
0 C:\Temp\ file1.txt
1 C:\Temp\ Test.txt
In [9]: pd.DataFrame.from_records(ft)
Out[9]:
Location Name
0 C:\Temp\ file1.txt
1 C:\Temp\ Test.txt
As you can see both pd.DataFrame(ft)
and pd.DataFrame.from_records(ft)
worked.如您所见,
pd.DataFrame(ft)
和pd.DataFrame.from_records(ft)
有效。
Also note that when you print the type list
, <class 'list'>
is what you get:另请注意,当您打印类型
list
, <class 'list'>
是您得到的:
In [14]: type(ft)
Out[14]: list
In [15]: print(type(ft))
<class 'list'>
如果你想得到dict类型的真实值,只需在发送到RPC客户端之前用pickle将响应序列化处理,再次用pickle反序列化后就可以得到dict类型的真实值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.