如何解决使用python读取屏幕上的文本
如何从文本文件中获取文本并将其保存为python中的变量?我尝试将其保存为图像,然后使用 PyTesseract.image_to_string() 但它似乎不起作用
解决方法
可以试试python的pickle模块:pickle documentation pickle example RealPython
我正在发布一个示例,不确定它是您要查找的内容,我的意图
来自'变量是存储数据值的容器'的意思:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pickle
# reads your textfile.txt and stores as 'variable'
with open("textfile.txt","r") as textfile:
variable=textfile.read()
print('\ntextfile.txt content: ',variable)
textfile.close()
#dumps the variable to a binary pickle serialized file
with open("variablefile.var",'wb') as variablefile:
pickle.dump(variable,variablefile)
#loads a new variable from the binary pickle serialized file
with open("variablefile.var",'rb') as variablefile:
variableloaded = pickle.load(variablefile)
print('variable loaded from file',variableloaded)
print('\nvariable loaded type from file',type(variableloaded))
或者您可以将数据存储在字典中并以 JSON 格式保存(JSON 代表 JavaScript Object Notation。这种格式是一种以键值排列存储数据的流行方法,以便以后可以轻松解析。){ {3}}
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
# create empty dictionary
variables_dict = {}
# reads your textfile.txt and stores as 'textfile.txt' inside the dictionary
with open("textfile.txt","r") as textfile:
variables_dict["text.file.txt"]= textfile.read()
print('\nvariables_dict : ',variables_dict)
textfile.close()
#save the dictionary as a json file 'variables.json'
with open('variables.json','w') as variablessaved:
json.dump(variables_dict,variablessaved)
#read the dictionary as a json file 'variables.json'
with open('variables.json','r') as variablesloaded:
loaded_dict = json.load(variablesloaded)
print('\nloaded dictionary : ',loaded_dict)
同样,我不确定您在寻找什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。