2

I'm kinda lost on how to use constant values from an external .txt file in my python file

File.txt:

email=value1
phone=value2
name=value3

And I want to use these values on my python file.

I have tried to do this:

Data = open('file.txt', 'r')
data1= Data.read(email)
print(data1)

But it isn't working as expected, expected output is "Value1". But I get an error, and if I only do Data.read() I am getting:

email=value1
phone=value2
name=value3

How would I get each value separately to use them in my python code?

3 Answers 3

1

I'm not sure exactly what are you looking for, but here's something that might help you:

# Open the file for read
Data = open('file.txt', 'r')

# Reading the data from file
data1 = {}
for line in Data.readlines():
    key, val = line.strip().split("=")  # strip() removes "\n" at the end of each line
    data1[key] = val                    # Placing key,val in dictionary


# Using the data
email = data1['email']

# Don't forget to close the file
Data.close()
Sign up to request clarification or add additional context in comments.

1 Comment

It's better to use with. Why? Read Python: Open a file using “open with” statement & benefits explained with examples article from thispointer.com.
0

Try this:

with open('file.txt', 'r') as file:
    data1 = dict(zip([i.strip().split('=') for i in file.readlines() if i]))
    print(data1['email'])

Output:

value1

4 Comments

File "test.py", line 2, in <module> data1 = dict(zip([i.strip().split('=') for i in file])) ValueError: dictionary update sequence element #0 has length 1; 2 is required Im getting this
@Pepee Can you show me your entire txt file?
its basically as i put in the post, file is named "file.txt" and inside is email=value1 phone=value2 name=value3 in 3 different lines, Python program called Test.py has exactly the code you provided
Still not working, But appreciate your help though. @ Almog answer works great
0

Extracted from @U12-Forward, working and tried to describe what we are doing:

with open('test.txt', 'r') as f:
    data = dict([i.split('=') for i in f.read().splitlines()])
    print(data["email"]) # value1
    print(data['phone']) # value2

What we are doing is, using f.read() reading file and splitting the f.read() into list from linebreak(\n) and looping through that list and again splitting those each lines with = and making list and then at last dict.

If it is harder for you to understand then you can split these lines like this, made easier to understand:

with open('test.txt', 'r') as f:
    read=f.read()
    data={}
    for i in read.splitlines():
        x=i.split("=")
        data.update(dict([x]))
    print(data["email"]) # value1
    print(data['phone']) # value2

Additionally:

You can use,change and do anything after this:

data["email"]="test@.."
print(data["email"]) # test@..

email=data["email"]
# Now you can use this email variable as your python variable.

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.