I'm currently learning Python and I'm having an issue with getting integer values from a text file (myfile.config). My goal is to be able to read a text file, find the integers then assign the said integers to some variables.
This is what my text file looks like (myFile.config):
someValue:100
anotherValue:1000
yetAnotherValue:-5
someOtherValueHere:5
This is what I've written so far:
import os.path
import numpy as np
# Check if config exists, otherwise generate a config file
def checkConfig():
if os.path.isfile('myFile.config'):
return True
else:
print("Config file not found - Generating default config...")
configFile = open("myFile.config", "w+")
configFile.write("someValue:100\rnotherValue:1000\ryetAnotherValue:-5\rsomeOtherValueHere:5")
configFile.close()
# Read the config file
def readConfig():
tempConfig = []
configFile = open('myFile.config', 'r')
for line in configFile:
cleanedField = line.strip() # remove \n from elements in list
fields = cleanedField.split(":")
tempConfig.append(fields[1])
configFile.close()
print(str(tempConfig))
return tempConfig
configOutput = np.asarray(readConfig())
someValue = configOutput[0]
anotherValue = configOutput[1]
yetAnotherValue = configOutput[2]
someOtherValueHere = configOutput[3]
One of the issues which I've noticed so far (if my current understanding of Python is correct) is that the elements in the list are being stored as strings. I've tried to correct this by converting the list to an array via the NumPy library, but it hasn't worked.
Thank you for taking the time to read this question.