Answers for "how to read a variable from a file in python"

13

python read file to variable

with open('data.txt', 'r') as file:
    data = file.read().replace('\n', '')
Posted by: Guest on February-27-2020
3

python read file to variable

# First, install PyVariable with - pip install pyvariable

import pyvariable

variable = pyvariable.LocalVariable()
variable.save("var_name", "value")  # Can be used any data type as variable value
var_name = variable.read_str("var_name")  # This will read the value of var_name

"""
Methods -
variable.read_int(var_name) for reading integer variable, 
variable.read_float(var_name) for reading float variable,
variable.read_str(var_name) for reading string variable,
variable.read_list(var_name) for reading list,
variable.read_tuple(var_name) for reading tuple,
variable.read_set(var_name) for reading set,
variable.read_dict(var_name) for reading dictionary,
variable.read_bool(var_name) for reading boolean variable,
variable.exists(var_name) to search if a variable exists,
variable.get_all_as_dict() to get all variables with value in dictionary format.
"""
Posted by: Guest on October-10-2021

Code answers related to "how to read a variable from a file in python"

Python Answers by Framework

Browse Popular Code Answers by Language