Here is my challenge. I have a radioConfig.py file which contains variable values which need to be changed if and when the user changes location or scan times. This would be used with students so i'm programming a GUI, pysimplegui, to change the values of those variable.
Here is what i have so far but its not working. Its replacing the variable name, not the value.
I'm using a Rpi and python3. I studied electronics and my program skills are with C. I'm not sure if this is the best way to solve my challenge nor do i know of the python options which exist that could be useful. Any help would be amazing.
#File: GuiTest.py before code is executed
freqCenter = 21000000
freqBandwidth = 8000000
upconvFreqHz = 125000000
fftWindow = "BlacHarr"
notes = "Test"
rtlSampleRateHz = 2000000
#-----------------------------------------------------------------------
#Program which will be a gui asking user for input values
freqCenterGUI = 20800280
with open('GuiTest.py', 'r') as file :
filedata = file.read()
filedata = filedata.replace('freqCenter', str(freqCenterGUI).strip('()'))
with open('GuiTest.py', 'w') as file:
file.write(filedata)
#File: GuiTest.py after code is executed
20800280 = 21000000
freqBandwidth = 8000000
upconvFreqHz = 125000000
notes = "Test"
rtlSampleRateHz = 2000000
#-----------------------------------------------------------------------
I'd say: use a config file.
Modifying a script is really not good practice !
In your cfg.ini:
[_]
freqCenter = 21000000
freqBandwidth = 8000000
upconvFreqHz = 125000000
fftWindow = BlacHarr
notes = Test
rtlSampleRateHz = 2000000
Then use configparser:
import configparser
cfg = configparser.ConfigParser()
with open('cfg.ini', encoding='utf-8') as f:
cfg.read_file(f)
cfg['_']['freqCenter'] = '20800280'
with open('cfg.ini', 'w', encoding='utf-8') as f:
cfg.write(f)
EDIT:
Or, as suggested by @juanpa.arrivillaga, using a json is also a great solution !
Matter of taste... :)
Quick and dirty:
File 1:
freqCenter = 21000000
freqBandwidth = 8000000
upconvFreqHz = 125000000
fftWindow = "BlacHarr"
notes = "Test"
rtlSampleRateHz = 2000000
File 2
#Program which will be a gui asking user for input values
freqCenterGUI = "20800280"
my_dict={}
file_data=open("GuiTest.py", "r")
in_lines=file_data.readlines()
for line in in_lines:
var, val = line.split('=')
if var.strip() == "freqCenter":
val = freqCenterGUI + "\n"
my_dict[var]=val
f_out=open("GuiTest.py", "w")
for key,val in my_dict.items():
f_out.write(str(key)+"="+str(val))
f_out.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.