在哪里放置大型Python列表 [英] Where to put large Python lists

查看:56
本文介绍了在哪里放置大型Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个python程序,该程序使用英语词典中所有单词的列表,所以我们说的是几兆字节.我想以一种干净的方式将列表作为常量包括在内,而不会妨碍源代码.我会将其放在JSON文件中,但我正在使用PySimpleGUI,并且我想利用其将代码转换为Windows EXE文件的能力,并且我怀疑它是否可以通过外部文本文件来实现.

I'm writing a python program that uses a list of all of the words in an English dictionary, so we're talking a few megabytes. I would like a clean way of including the list as a constant without it getting in the way of the source code. I would put it in a JSON file or something, but I'm using PySimpleGUI, and I want to take advantage of its ability to turn the code into a Windows EXE file, and I doubt it could do that with an external text file.

因此,按照优先顺序,我想执行以下操作之一:

So, in order of preference I would like to do one of the following:

  1. 如果PySimpleGui的exe-maker支持,则将其保存在外部文本文件中.
  2. 将其保存在另一个python脚本中并导入.
  3. 将其保存在原始的python脚本中,但在顶部声明,但实际上在底部填充,不会妨碍查看源代码.
  4. 在源文件的顶部使用所有值和所有内容声明它,每当我查看源代码时,都向下滚动到底部.

解决此问题的最佳方法是什么?我想念一种聪明的方法吗?

What's the best way to tackle this? Is there a clever way that I am missing?

推荐答案

我个人认为将数据保存在单独的文件中是最干净的方法.例如,您可以仅从另一个应用程序创建 csv json 或简单文本文件,然后将其放置在正确的目录中.

I personally think having the data in a separate file is the cleanest way. You could for example just create a csv or json or simple text file from another application and just place it in the right directory.

似乎pysimpleGUI本身并不是创建可执行文件,它似乎正在使用 pyinstaller ( https://pysimplegui.readthedocs.io/en/最新/#creating-a-windows-exe文件)

It seems, that pysimpleGUI does not create executables by itself, it seems to be using pyinstaller ( https://pysimplegui.readthedocs.io/en/latest/#creating-a-windows-exe-file )

Pyinstaller允许在其可执行文件中添加非python文件(例如,在您的情况下为json或csv文件).

Pyinstaller allows to add non python files (like for example in your case json or csv files) to its executable.

启动可执行文件后,将创建一个临时目录,所有python和添加的文件将被提取并存储在其中.

When the executable is started a temporary directory is created and all the python and the added files will be extracted and stored there.

您必须使用开关-add-data * source_path *:** destination_path **

下面的示例演示如何执行此操作.

Below example shows how to do this.

只需在安装了python脚本的目录中创建一个名为 files 的目录,然后将要与应用程序捆绑在一起的数据文件放在其中.

just create in the directory where your python script is installed a directory named files and place your datafiles you want to be bundled with your application inside it.

在此示例中,这将是一个名为 files/info.csv

For this example this will be one file named files/info.csv

为使此示例正常工作,它应至少包含一行.

For this example to work it should contain at least one line.

然后创建您的python文件(此测试使用以下内容)

Then create your python file (for this test with following contents)

示例文件名:sg_w_fread.py

import os
import sys

import PySimpleGUI as sg

if getattr(sys, "frozen", False):
    # for executable mode
    BASEDIR = sys._MEIPASS  
else:
    # for development mode
    BASEDIR = os.path.dirname(os.path.realpath(__file__))

print("BASEDIR =", BASEDIR)


fname = os.path.join(BASEDIR, "files", "info.csv")

# just for debugging
print("FNAME", repr(fname))
print(os.path.isfile(fname))

sg.theme('DarkAmber')   # Add a touch of color

# read something from a file
with open(fname) as fin:
    txt = fin.read().split("\n")[0]

layout = [  [sg.Text("first line of file is '" + txt + "'")],  # display something from the file
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')] ]

window = sg.Window('Window Title', layout)

while True:
    event, values = window.read()
    if event in (None, 'Cancel'):   # if user closes window or clicks cancel
        break
    print('You entered ', values[0])

window.close()

并键入以下命令以创建可执行文件:

and type following command to create the executable:

pyinstaller -wF sg_w_fread.py --add-data files:files

这篇关于在哪里放置大型Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆