Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Saving data from 1 cell to the next in Jupyter notebook

enter image description here

I’m working in a Jupyter notebook. I want to open a kml file in 1 cell and do some analysis in the next. After selecting a file , the first cell has:

# Print the selected path, filename, or both
print(fc.selected_path)
print(fc.selected_filename)
print(fc.selected)
global mytext 
with open(fc.selected, 'r') as f:
    print(f.read())
    mytext = f.read()

This prints out the contents of the file as expected.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In the next cell , I try to print out my text , but I get an empty string.

How do I pass the contents of the file to the my text variable in the next cell?

>Solution :

f.read() exhausts a file and leaves the cursor at the end of the file, therefore mytext doesn’t receive any more content.

Try reading first and then printing:

with open(fc.selected, 'r') as f:
    mytext = f.read()
    print(mytext)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading