如何修复读取文件def中的'if bool'语句

时间:2019-11-12 11:32:07

标签: python python-3.x

因此,我尝试制作一个应用程序来帮助朋友做日常工作,并且我创建了一种设置和语言,以便更轻松地更改某些应用程序属性,并且它们都具有读取某些文本文件的相同想法,但是是工作而其他人不是:(

我试图删除选项卡标题翻译并且可以正常工作,但是当我将它们放回原处时,py将不起作用。

无法读取语言文件

#// Try to read language file
langf = "asi_lang_"
lange = ".kmc"

app_title       = ""
tab_dimensions  = ""
tab_materials   = ""
tab_settings    = ""

def readFileLanguage(langName):
    global app_title, tab_dimensions, tab_materials, tab_settings

    file = fr"res\lang\{langf}{langName}{lange}"
    try:
        with open(file, mode="r", encoding="utf-8") as r:
            l = r.readline()

            while l:
                if "=" in l:
                    x = l.split("=")
                    data=x[0]; info=x[1]
                    if "\n" in info:
                        info = info.replace("\n", "")

                    if data == "app_title":
                        app_title = info
                        l = r.readline()
                    elif data == "tab_dimensions":
                        tab_dimensions = info
                        l = r.readable()
                    elif data == "tab_materials":
                        tab_materials = info
                        l = r.readable()
                    elif data == "tab_settings":
                        tab_settings = info
                        l = r.readable()

                    else:
                        l = r.readline()
                else:
                    l = r.readline()
    finally:
        r.close()

语言文件(asi_lang_ro.kmc)如下所示:

#// KmcASI language file

app_title=Testare App

#// If I delete this lines will work but I need them
tab_dimensions=Dimensiuni
tab_materials=Materiale
tab_settings=Setari

#// End file

控制台输出:

Traceback (most recent call last):
  File "C:/Users/Andrei/PycharmProjects/Testing/main.py", line 10, in <module>
    readFileLanguage(app_language)
  File "C:\Users\Andrei\PycharmProjects\Testing\res\lib\language.py", line 19, in readFileLanguage
    if "=" in l:
TypeError: argument of type 'bool' is not iterable

我希望有些标签可以从语言文件中获取名称。如果我删除它们,将可以使用,但不会在该选项卡上显示任何文本。

1 个答案:

答案 0 :(得分:1)

如我的评论中所述,您的问题是您使用的是readable()而不是readline()。可读返回布尔值TrueFalse,这使l成为布尔类型,这就是为什么下一次执行"=" in l时会出现此错误的原因。然而,查看代码,您将设置几个单独的变量。为了使它具有可伸缩性和动态性,最好将设置存储在哈希中。这样,如果您在文件中添加新设置,则无需更新代码即可将其加载到变量中。它们将自动加载到哈希中。像下面这样的示例作为处理此问题的更动态方式的示例。

#// Try to read language file
langf = "asi_lang_"
lange = ".kmc"


def readFileLanguage(langName):
    #hardcoded the file for test purpose.
    file = fr"data.dat"
    app_settings = {}
    with open(file, mode="r", encoding="utf-8") as lines:
        for line in lines:
            if "=" in line:
                key, value = line.rstrip().split('=')
                app_settings[key]=value
    return app_settings


app_settings = readFileLanguage("test")
print(app_settings)
print(f'The value of app_title is {app_settings["app_title"]}')

输出

{'app_title': 'Testare App', 'tab_dimensions': 'Dimensiuni', 'tab_materials': 'Materiale', 'tab_settings': 'Setari'}
The value of app_title is Testare App