-7

I'm reading from a text file in binary using python pickle, and it reads a number but this number is read as a string, and I want to use it to multiply, can I convert the str to an int value?

saveln = input("Please enter score card: ")
try:
    open_file = open(saveln + '.txt', 'rb')
    load_file = pickle.load(open_file)

    print(load_file['name']+" score card: ""\n")
    print("score: "+load_file['score']+"\n")
    open_file.close()
except:
    print("\n score card does not exist!!")

so how would I convert the 'score' into an Integra value?

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
  • 1
    you can use int(your string) method to convert a string to int – therealprashant Mar 04 '15 at 12:20
  • 1
    but technicaly, you're not using a text file - you're using pickled binary representation of some python data... It would be good to read something about reading text files and leave pickling for when you'll feel more comfortable in python.. – Jan Spurny Mar 04 '15 at 12:23
  • 1
    Why are you using this blanket exception that obliterates a useful error message and replaces it with a useless one that is printed to the wrong stream? If you want to suppress the stack trace, do: – William Pursell Mar 04 '15 at 12:25
  • `try: ... except Exception as e: raise SystemExit(e)` – William Pursell Mar 04 '15 at 12:26

2 Answers2

0

Just use int:

int(load_file['score'])
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0

You can do it like this: int(your_string)

Timur Osadchiy
  • 5,699
  • 2
  • 26
  • 28