Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################################################
- # with JSON
- ###################################################
- import json
- #--- write ---
- Var1 = 1
- Var2 = 2
- data = [Var1, Var2]
- with open("MyFile.txt", "w") as TxtFile:
- json.dump(data, TxtFile)
- #--- read ---
- with open("MyFile.txt") as TxtFile:
- data = json.load(TxtFile)
- Var1 = data[0]
- Var2 = data[1]
- print(Var1, type(Var1)) # 1 <class 'int'>
- print(Var2, type(Var2)) # 2 <class 'int'>
- ###################################################
- # without JSON
- ###################################################
- # --- write ---
- Var1 = 1
- Var2 = 2
- with open("MyFile.txt", "w") as TxtFile:
- TxtFile.write(str(Var1))
- TxtFile.write('\n')
- TxtFile.write(str(Var2))
- TxtFile.write('\n')
- # --- read ---
- with open("MyFile.txt") as TxtFile:
- Var1 = int(TxtFile.readline())
- Var2 = int(TxtFile.readline())
- print(Var1, type(Var1)) # 1 <class 'int'>
- print(Var2, type(Var2)) # 2 <class 'int'>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement