简体   繁体   中英

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.

However, when this is returned back to client, the list object (fl) is of type

<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.

I'd like this to be converted to a dataframe but using ' df = pd.DataFrame(fl) ' returns an error 'AttributeError: cannot access 'keys'

RPyC server function:

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.

Also note that when you print the type list , <class 'list'> is what you get:

In [14]: type(ft)
Out[14]: list

In [15]: print(type(ft))
<class 'list'>

如果你想得到dict类型的真实值,只需在发送到RPC客户端之前用pickle将响应序列化处理,再次用pickle反序列化后就可以得到dict类型的真实值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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