0

I'm trying to add saves to my code. The loading works, and I am able to write the strings into the text file, but I can't figure out how to write an integer (number) into the txt file.

I've tried to define the integers as strings but none have worked.

(Variable names are in Norwegian.)

def save():
    with open("a.txt") as f:
        f.write(int(penger))
        f.write(int(vognplass_list[0]))
        f.write(int(vognplass_list[1]))
        f.write(int(vognplass_list[2]))
        f.write(str(vognplasser[0]))
        f.write(str(vognplasser[1]))
        f.write(str(vognplasser[2]))

The error says:

TypeError: write() argument must be str, not int
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Is there a specific reason why they *must* be `int`s? Based on your explicit `int(...)` conversion, I'd assume the items are strings, which is the type that must be used for `write` already – emirps Feb 07 '23 at 14:22
  • Does this answer your question? [TypeError: expected a character buffer object](https://stackoverflow.com/questions/24439988/typeerror-expected-a-character-buffer-object) – Axe319 Feb 07 '23 at 14:22
  • its actually partly bad code, and in my attempts to fix the code myself, i assigned the ints with str and also with int. and they must be ints as they are barely used for printing and mainly used with math – Edvard Berger Feb 07 '23 at 14:26
  • Files *only* contain bytes; `f.write` expects a `str` so that it can use `str.encode` to produce the necessary bytes to write to the file, but it doesn't go a step further and try to call `str` on any non-`str` arguments first. – chepner Feb 07 '23 at 14:39
  • The `print` function will make implicit calls to `str`, but that's because it's intended to be a convenient wrapper around `sys.stdout.write`. `write` is the "disciplined" function, whose only convenience is making implicit calls to `encode` when the underlying file was opened in text mode. – chepner Feb 07 '23 at 14:40

3 Answers3

1

The error message explains the problem:

TypeError: write() argument must be str, not int

While you're explicitly calling the int constructor:

f.write(int(penger))

Text files don't story any information about what is in the file, only the strings that are in the file. So a possible solution would be:

f.write(str(penger))

But do note that the type information of penger is lost when trying to read the text file later.

1

As the error message says, f.write takes a str, not an int, so you need to convert your data to a string form in order to write it to the file. You then need to convert it back from str to int after loading it from the file.

Rather than reinventing the wheel, I'd suggest using the json module for this, which handles ints as well as nested lists:

import json

def save(penger, vognplass_list, vognplasser):
    with open("a.txt", "w") as f:
        json.dump([penger, vognplass_list, vognplasser], f)

You can then use json.load to read the data back, and it will be automatically converted into its original form (i.e. whatever type of object you passed as the argument to json.dump, in this case a list with an int and two more lists):

def load():
    # loads penger, vognplass_list, vognplasser from a.txt
    with open("a.txt") as f:
        return json.load(f)

and you can destructure that back into the original three variables:

penger, vognplass_list, vognplasser = load()
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

python write supports only str writing so you need to explicitly convert int into str. Since python is open source maybe if you want to you can modify the source code for write function or build a user defined module/package as per your usage needs

besides when writing a txt file all of the input buffer is written as binary first so the concept of int or str file during writing is meaningless.

also add a 'w' write mode modifier in the open method here is my code

> def save():
>     with open("a.txt",'w') as f:
>         f.write(str(penger))
>         for i in range(0,len(vognplass_list)): 
>             f.write(str(vognplass_list[i]))
>         for i in range(0,len(vognplasser)):
>             f.write(str(vognplasser[i]))
Vishnu Balaji
  • 175
  • 1
  • 11
  • thanks for this answer. its what allowed me to do it, tho it wasnt the hole answer, but the fact that i didnt know to add the "w" in line 2 – Edvard Berger Feb 07 '23 at 19:15