Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

pyautogui - Reading the text on screen using python

How do I get the text from a text file and save it as a variable in python? I tried saving it as an image and then using the pytesseract.image_to_string() but it did not seem to work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try the pickle module of python: pickle documentation pickle example RealPython

I am posting an example, not sure it is what you are looking for, my intents

came from the 'Variables are containers for storing data values' meaning:

#!/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('
textfile.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('
variable loaded type from file', type(variableloaded))
    

or You can store your data in a dictionary and save them in JSON format (JSON stands for JavaScript Object Notation. This format is a popular method of storing data in key-value arrangements so it can be parsed easily later.) python JSON documentation

#!/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('
variables_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('
loaded dictionary : ' , loaded_dict)

Again I am not sure about what you were looking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...