1

I want to import an text file to python script, and then do an if condition, something like this:

Let's say that I have this in example.txt file:

os: ubuntu
required: no

And I want to do this:

if os =="ubuntu" and if required== "no":
 (exec terminal command);
elif os =="debian" & if required== "yes":
 (exec another terminal command);

Ignore syntax errors, it was only for you to understand.

EDIT

Thanks to @zyad osseyran, I managed to get this.

f = open("example.txt", "r")

for x in f:
    x = x.split(':') 
    atribute = x[0]
    value = x[1]

How can I make this, turning into and dictionary? And, how to get the values from here, to make an IF Condition?

6
  • Show what you tried as properly formatted code in the question. Commented Jul 12, 2020 at 16:02
  • 1
    It seems like you can do some programming in Python. Now go ahead, read the file line by line, split it on the separation character, assign the variables and you're good to go. Commented Jul 12, 2020 at 16:03
  • @MichaelButscher nothing relevant so far, giving it a try. Commented Jul 12, 2020 at 16:04
  • 1
    @ThomasWeller I can, only basic python, but will give a try on what you said. Thank you Commented Jul 12, 2020 at 16:05
  • 1
    @kyriakosSt I understand that, Thank you! Edited the post by the way, thank you all Commented Jul 12, 2020 at 17:28

2 Answers 2

1

In order to save the values to a dictionary, you can do the following:

config = dict()                   # construct an empty dictionary. Fill it with key-value pairs a every iteration
f = open("demofile.txt", "r")
for x in f:
   x = x.split(':') 
   attribute = x[0].strip()       # .strip() removes preceding and trailing whitespaces
   value = x[1].strip()
   config[attribute] = value      # save the attribute and its value to the dictionary

In this case config is the dictionary that has all values of assigned to attribute as keys and the corresponding value's as values. I have also added the .strip() method to the items you read in order to remove any whitespaces (since the way your example.txt is formatted, x[1] will have values " ubuntu" and " no" rather than "ubuntu" and "no").

Now you can construct your if statement like this:

if config['os'] == 'ubuntu' and config['required'] == 'no':
    # exec terminal command
elif config['os'] == 'debian' and config['required'] == 'yes':
    # exec another terminal command
Sign up to request clarification or add additional context in comments.

Comments

1
    f = open("demofile.txt", "r")

    for x in f:
      x = x.split(':') 
      attribute = x[0]
      value = x[1]

https://www.w3schools.com/python/python_file_open.asp

https://www.w3schools.com/python/ref_string_split.asp

Just order them in a nice dictonary and you can do your if statements https://www.w3schools.com/python/python_dictionaries.asp

1 Comment

okay, that gave me some help, now, the thing is. I don't get how can I make that turning into an dictionary. Can you give me some tips? Thanks in advance

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.