I have loaded a csv file and as i try to print it i get this error
Traceback (most recent call last):File "C:\Users\FSTC\Downloads\spaceproject\main.py", line 389, in <module>world_data[x][y]= int(tile)
ValueError: invalid literal for int() with base 10: '-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-
I am suspecting my program but don't know where exactly is the problem.
This is where I loaded and called it:
world_data = []
for row in range(ROWS):r = [-1] * COLSworld_data.append(r)
# load in level data and create world
with open(f"level{level}.csv", newline="") as csvfile:reader = csv.reader(csvfile, delimiter=",")for x, row in enumerate(reader):for y, tile in enumerate(row):world_data[x][y]= int(tile)print(world_data)
variable declaration
level = 1
ROWS = 16
COLS = 150
TILE_SIZE = screen_height // ROWS
TILE_TYPES = 21
[[-1][-1][1]],[-1][1][-1][-1]]
[[-1][-1][1]],[-1][1][-1][-1]]
in the first for loop, i want to get all the items in the rows.. then in the second for loop i want to get each particular item or number in a row which i called a tile. tha's just an excerpt of my main csv file.
If you have a .csv
file with rows like this...
[[-1][-1][1]],[-1][1][-1][-1]]
[[-1][-1][1]],[-1][1][-1][-1]]
You'll read in each row as a list of strings when you call this in your code:
for x, row in enumerate(reader):
So now, each row
would look like...
['[[-1][-1][1]]', '[-1][1][-1][-1]]'] # row 1
['[[-1][-1][1]]', '[-1][1][-1][-1]]'] # row 2
Do you see what the problem is here? The internal items are still strings, but you're trying to treat them as lists when you do this:
for y, tile in enumerate(row):
You would be iterating over the '[[-1][-1][1]]'
and '[-1][1][-1][-1]]'
strings because the CSV reader doesn't do any sort of type translation from str
to Python objects, so when calling int(tile)
you're actually doing something like this:
int('[[-1][-1][1]]')
What you need to do instead is to convert those strings in your CSV into the individual elements.
One way to do this would be to remove all brackets from your string and have only the integers inside them, and for that you could re.split()
the string on [
and ]
and then iterate through just the numbers:
import csv
import re# just making our dummy board of 2 x 7
# for sample tiles.csv
world_data = []
world_data.append([None] * 7)
world_data.append([None] * 7)# load in level data and create world
with open("tiles.csv", newline="") as csvfile:reader = csv.reader(csvfile, delimiter=",")for x, row in enumerate(reader):# I don't know how your 'rows' actually are supposed# to be used since each 'row' in your csv is# a list of lists, but I assume each row is just all the# columns so I'm joining everything togethercols = ''.join(row)# this filters out the None values# from splitting a stringtiles = (t for t in re.split(r"\[|]", cols) if t)for y, tile in enumerate(tiles):world_data[x][y]= int(tile)print(f"world_data: {world_data}")
And this should work and output...
world_data: [[-1, -1, 1, -1, 1, -1, -1], [-1, -1, 1, -1, 1, -1, -1]]
And for reference, my tiles.csv
is just a file that looks like this:
> cat tiles.csv
[[-1][-1][1]],[-1][1][-1][-1]]
[[-1][-1][1]],[-1][1][-1][-1]]