config.yml 示例,
DBtables:
CurrentMinuteLoad:
CSV_File: trend.csv
Table_Name: currentminuteload
GUI 图像,
这可能不是最干净的路线。
我正在制作一个 GUI,为我正在使用的另一个 python 脚本创建 config.yml 文件。
使用 pysimplegui,我的按钮未按我预期的方式运行。它当前准确地检查引用名称(此处的示例为 CurrentMinuteLoad),如果存在,则会将其踢回,但会跳过对表的检查(因此会跳过 ELIF 语句)。添加表格仍然有效,我只是没有得到我想要的双重检查。另外,我必须在 GUI 中按两次“确定”按钮才能正常工作?这是一个奇怪的怪癖,对我来说不太有意义。
def add_table():
window2.read()
with open ("config.yml","r") as h:
if values['new_ref'] in h.read():
sg.popup('Reference name already exists')
elif values['new_db'] in h.read():
sg.popup('Table name already exists')
else:
with open("config.yml", "a+") as f:
f.write("\n " + values['new_ref'] +":")
f.write("\n CSV_File:" + values['new_csv'])
f.write("\n Table_Name:" + values['new_db'])
f.close()
sg.popup('The reference "' + values['new_ref'] + '" has been included and will add the table "' + values['new_db'] + '" to PG Admin during the next scheduled upload')
最佳答案
当您使用h.read()
时,您应该保存该值,因为它将像流一样读取该值,并且后续调用此方法将导致空字符串。
尝试像这样编辑代码:
with open ("config.yml","r") as h:
content = h.read()
if values['new_ref'] in content:
sg.popup('Reference name already exists')
elif values['new_db'] in content:
sg.popup('Table name already exists')
else:
# ...
关于python - 检查文件中的重复项两次 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71954978/