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

How do I treat strings separated by spaces in text files?

I have a text file containing a string of ‘0’s and ‘1’s, followed by a space and another string of ‘0’s and ‘1’s of the same length. What would be the easiest way of storing the string before the space and the string after in two different variables?

Let’s say I have a text file called Text.txt. How would I store the first string in the variable ‘encryptedText’ and the second string in the variable ‘key’?

file = open("Text.txt", mode="r")
encryptedText = ""
key = ""

file.close()

What’s in the text file (all on the first line): 01001001010 11000100111

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

01001001010 should be stored in ‘encryptedText’, and 11000100111 should be stored in ‘key’.

>Solution :

easiest way is to split on space

with open("Text.txt", mode="r") as file:
    encryptedtext, key = file.readline().split(' ')
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