Search code examples
pythoninputdata-collection

Python: Data Getting


Im trying to find out how to get certain data from a file in the easiest way possible. I have searched all over the internet but can't find anything. I want to be able to do this:

File.txt:

data1 = 1
data2 = 2

but i want to get only data1 like so,

p = open('file.txt')
f = p.get(data1)
print(f)

Any Ideas, Thanks in advance.


Solution

  • with open("file.txt", "r") as f:
            key, val = f.readline().split('=')
            if key.strip() == 'data1':  # if data1 is not the first line
                # do something with value and data