-1

I want to read my parameter file line by line and save the values into a variable my parameter file look something like that

Parameter File

DBHOSTNAME=192.168.x.x
DBSID=SID
BEUSERNAME=username
BEUSERPASSWORD=password
HOSTPATHBE=path

Code

file = open('envparam.config')
    for line in file:
        fields = line.strip().split()
        print (fields[0])

So far I am able to read my parameter file but not able to store values into variables can anyone help me with this

Scaarus
  • 81
  • 4
Fahad
  • 13
  • 1
  • 4
  • What have you written to try and "store values into a variables"? What would you want the result of executing your program to be? – Scott Hunter Mar 30 '19 at 22:48
  • @Scott Hunter my program should should store the values of DBHOSTNAME,DBSID,BEUSERNAME,BEUSERPASSWORD&HOSTPATHBE – Fahad Mar 30 '19 at 22:51
  • store them into a https://docs.python.org/3/tutorial/datastructures.html#dictionaries – Patrick Artner Mar 30 '19 at 22:52
  • possible duplicate of [Parse key value pairs in a text file](https://stackoverflow.com/questions/9161439) – eyllanesc Mar 30 '19 at 23:47

2 Answers2

2

If your file is consistent, this will store your info in a dictionary:

with open('envparam.config') as f:
    data = {}
    for line in f:
        key, value = line.strip().split('=')
        data[key] = value

You can then access it like this:

>>> data['DBSID']
SID
Merig
  • 1,751
  • 2
  • 13
  • 18
-1

To create a list with a line as each element, use the .split() method with "\n"(the newline character)

file = open('envparam.config').split('\n')
Amber Cahill
  • 106
  • 8