从外部 python 文件中检索 python 列表 (Django)
Retrieve a python list from external python file(Django)
我正在尝试检索从外部 python 文件 (algorithm.py
) 生成的 python 列表。在 algorithm.py
中有一组用于生成 python 列表的函数。 algorithm.py
文件没有问题,它会按我的预期生成 python 列表。我按照以下步骤将 algorithm.py
中生成的 python 列表获取到 views.py
中。
1.) 将变量(lec_name
)从 views.py
中的函数传递到 algorithm.py
并使用 stdout
检索输出,如下面的语句。
lec_name = request.POST['selected_name']
result = run([sys.executable,'//Projects//altg//algorithm.py',lec_name], shell=False, stdout=PIPE)
2.) 然后当我 print(result.stdout)
时,我收到输出( 我期望 的 python 列表)作为 array of byte
如下。
b"[['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]\r\n"
3.) 然后我用 print((result.stdout).strip())
删除了 \r\n
并且输出如下。
b"[['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]"
这给出了我期望的 python 列表 array of byte
所以我需要的是检索输出( 我期望的 python 列表) 作为 python list
。我该如何解决这个问题?我尝试了很多东西,但 none 成功了。我正在使用 Python 3.7.4,Django 3.0.1
尝试使用 literal_eval
,其中 Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression:
import ast
data = b"[['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]"
data_list = ast.literal_eval(data.decode())
print(type(data_list)) # output: <class 'list'>
print(data_list) # output: [['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]
我正在尝试检索从外部 python 文件 (algorithm.py
) 生成的 python 列表。在 algorithm.py
中有一组用于生成 python 列表的函数。 algorithm.py
文件没有问题,它会按我的预期生成 python 列表。我按照以下步骤将 algorithm.py
中生成的 python 列表获取到 views.py
中。
1.) 将变量(lec_name
)从 views.py
中的函数传递到 algorithm.py
并使用 stdout
检索输出,如下面的语句。
lec_name = request.POST['selected_name']
result = run([sys.executable,'//Projects//altg//algorithm.py',lec_name], shell=False, stdout=PIPE)
2.) 然后当我 print(result.stdout)
时,我收到输出( 我期望 的 python 列表)作为 array of byte
如下。
b"[['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]\r\n"
3.) 然后我用 print((result.stdout).strip())
删除了 \r\n
并且输出如下。
b"[['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]"
这给出了我期望的 python 列表 array of byte
所以我需要的是检索输出( 我期望的 python 列表) 作为 python list
。我该如何解决这个问题?我尝试了很多东西,但 none 成功了。我正在使用 Python 3.7.4,Django 3.0.1
尝试使用 literal_eval
,其中 Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression:
import ast
data = b"[['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]"
data_list = ast.literal_eval(data.decode())
print(type(data_list)) # output: <class 'list'>
print(data_list) # output: [['0', 'Subject01(IS0001)', 'Z Hall(65)', 'Thursday 10:00-12:00'], ['1', 'Subject02(IS42255)', 'N Hall(90)', 'Wednesday 08:00-10.00']]