The properties file stores the information as Key and Values. Here Key is USER_NAME, PASSWORD, IP_ADD and Value is fenfnewl, fnaofna, ftp.internal.com
I wrote python script to extract info using a regex and printed the output. The output looks like
USER_NAME = fenfnewl
PASSWORD = fnaofna
IP_ADD = ftp.internal.com
I have variables V_USER_NAME, V_PASSWORD, V_IP_ADD. I want these variables to get the values as follows
V_USER_NAME = fenfnewl
V_PASSWORD = fnaofna
V_IP_ADD = ftp.internal.com
The code that i wrote to get the output from properties file is as follows and it works fine. I just want to change the Keys from USER_NAME to V_USER_NAME in the output.
#!/usr/bin/env python
import re # import the regular expressions module
import sys
import mysql.connector
val = 'DEFAULT'
print val
REG_EXP={'DEFAULT':r"\bDEFAULT_(USER_NAME|PASSWORD|IP_ADD)\w*\b",
'NOT_DEFAULT':r"\bNOT_DEFAULT_(USER_NAME|PASSWORD|IP_ADD)\w*\b"}
filename = "/Users/DOCUMENTS/some.properties"
if val != 'DEFAULT':
pattern = re.compile(REG_EXP['NOT_DEFAULT'], re.IGNORECASE)
with open(filename, "rt") as in_file:
for line in in_file:
if pattern.search(line) != None:
print(line)
else:
pattern = re.compile(REG_EXP['DEFAULT'], re.IGNORECASE)
with open(filename, "rt") as in_file:
for line in in_file:
if pattern.search(line) != None:
print(line)
I am a beginner to python. Any help is much appreciated. Thanks in advance
v_added to each key name)? Or do you mean you want to create python variables with variable names and values defined by the first bit of output? That is, if the output werefoo = bar, do you want to create a variable namedv_foowith a string value of"bar"?