here is my code right now:
from pathlib import Path
content = Path('numbers.txt').read_text()
for i in range(len(content)):
content[i] = int(content[i])
print(content)
the code doesn't work as of now
here is my code right now:
from pathlib import Path
content = Path('numbers.txt').read_text()
for i in range(len(content)):
content[i] = int(content[i])
print(content)
the code doesn't work as of now
Should be simple with split, unless I'm misunderstanding?
from pathlib import Path
content = Path('numbers.txt').read_text()
list_content = content.split()
integer_list = [int(x) for x in list_content]
print(integer_list)
from pathlib import Path
content = Path('numbers.txt').read_text()
listofnumbers = list(map(int, content.split()))
This code works by mapping the int function to the list of numbers in content.
Edit: Had a bug in my code it should be fixed now.
Unfortunately you can't create a list of integers, but you can create a list of 'numbers'. If that works, you could try the following:
from pathlib import Path
content = Path('numbers.txt').read_text().split(" ")
print(content)
The split(" ")
splits the numbers in the file by the space in the quotes. Then it adds each of these 'splits' to an array.
If you need to keep it as an integer, you could instead use something like this:
content = Path('numbers.txt').read_text().split(" ")
for n in content:
print((int(n))
# Execute your code here but use the int() method to convert it back to an integer
For this you'll need to execute your code each time it splits the numbers.