简体   繁体   中英

How to return any object with sys.stdout

I am working on a python bash script and I would like to output a list from the first file to the next as so:

sender.py

#!/usr/bin/env python
import sys
l = [1,2,4,6,8,"maybe some text"]
sys.stdout.write(Qs)
sys.stdout.flush()

receiver.py

#!/usr/bin/env python
import sys
print(sys.stdin.readline())

then do in bash

./sender_py | ./receiver

I know the problem is that this method can only read in a string, but is there any other way to pass any object. In this case, I am passing a list, but later I would like to try other objects such as pandas dfs.

You can use data serialization with pickle :

sender.py

#!/usr/bin/env python
import sys
import pickle
l = [1,2,4,6,8,"maybe some text"]
sys.stdout.buffer.write(pickle.dumps(l))

receiver.py

#!/usr/bin/env python
import sys
import pickle
print(pickle.loads(sys.stdin.buffer.read()))

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