0

I am using a notebook (in Google Colab) in python 3 and really need to execute some python2 code with some data that is generated in my notebook !

So what I did was

  1. Generate the data in my cells AND save it to a file (data.txt)
  2. Write a python 2 script (myScript.py) with a main() function that parses the file from sys.argv[1] into data and than calls my python2 functions and do all the stuff then it ends with return results (which is a dictionary)
  3. In my notebook I run
!python2 myScript.py ./data.txt

(They are of course all in the same directory)

The command runs with no errors but no output ! How do I catch the results that are returned in a variable that I could use later ?


Not important but could be helpful :

Is there a better way to actually achieve what I am willing to achieve ?

2
  • 1
    On possibility would be to make your python script write to stdout and read it back as a string: myvar = !python2 myScript.py ./data.txt myScript.py should write to stdout using print or similar. ` def main(): print('Hello World') ` If you have a dictionary, you could either use eval() or print it csv like and parse back to a dict Commented Jun 16, 2020 at 13:30
  • 1
    Thank you for your answer, I didn't know we could do myvar = !python2 myScript.py ./data.txt, I used that insight to dump my dictionary as JSON then loading it from my notebook. Thank you for your help Commented Jun 16, 2020 at 14:50

1 Answer 1

1

Thanks to @Manuel's comment on the question, I figured out this solution and it worked :

  1. In myScript.py, I changed return results by sys.stdout.write(json.dumps(results))
  2. In my notebook, I changed the cell that executes the script to this:
results = !python2 test_langdetect.py ./tmp_comments.txt
myVar = json.loads(results[0])

Of course you need to import json

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.