简体   繁体   English

我通常会收到此错误: ValueError: invalid literal for int() with base 10

[英]i usually get this error : ValueError: invalid literal for int() with base 10

I have loaded a csv file and as i try to print it i get this error我已经加载了一个 csv 文件,当我尝试打印它时,我收到了这个错误

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] * COLS
    world_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.在第一个 for 循环中,我想获取行中的所有项目。然后在第二个 for 循环中,我想获取行中的每个特定项目或数字,我称之为 tile。 tha's just an excerpt of my main csv file.这只是我的主要 csv 文件的摘录。

If you have a .csv file with rows like this...如果您有一个.csv文件,其中包含这样的行...

[[-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...所以现在,每一row看起来像......

['[[-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:您将遍历'[[-1][-1][1]]''[-1][1][-1][-1]]'字符串,因为 CSV 阅读器不执行任何操作从str到 Python 对象的类型转换,所以当调用int(tile)你实际上是在做这样的事情:

int('[[-1][-1][1]]')

What you need to do instead is to convert those strings in your CSV into the individual elements.您需要做的是将 CSV 中的这些字符串转换为单独的元素。

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:做到这一点的一种方法是从字符串中删除所有括号并在其中仅包含整数,为此您可以re.split() []上的字符串,然后仅遍历数字:

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 together
        cols = ''.join(row)
        
        # this filters out the None values
        # from splitting a string
        tiles = (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...这应该可以工作,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:作为参考,我的tiles.csv只是一个看起来像这样的文件:

> cat tiles.csv
[[-1][-1][1]],[-1][1][-1][-1]]
[[-1][-1][1]],[-1][1][-1][-1]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到此错误: ValueError: invalid literal for int() with base 10: '\n' - I get this error: ValueError: invalid literal for int() with base 10: '\n' 错误 - ValueError:基数为10的int()的文字无效:&#39;&#39; - Error - ValueError: invalid literal for int() with base 10: ' ' 错误:ValueError:int()以10为底的无效文字: - Error: ValueError: invalid literal for int() with base 10: '' ValueError:int() 的无效文字,基数为 10:&#39;&#39; 错误 - ValueError: invalid literal for int() with base 10: '' error Kaprekar数字:我得到ValueError:以10为底的int()的无效文字&#39;&#39; - Kaprekar numbers: I Get ValueError: invalid literal for int() with base 10 '' 我不断收到此错误: ValueError: invalid literal for int() with base 10: '' - I keep getting this error: ValueError: invalid literal for int() with base 10: '' 当我运行以下代码时,出现以下错误:ValueError:int()以10为底的无效文字:“((1,0,&#39;Friday&#39;)” - When I run the following code,I get this error:ValueError: invalid literal for int() with base 10: “(1, 0, 'Friday')” 我如何摆脱此追溯错误:ValueError:以10为底的int()的无效文字:&#39;&#39; - How can i get rid of this traceback error: ValueError: invalid literal for int() with base 10: '' 获取错误- year = int(year_field.get()) ValueError: invalid literal for int() with base 10: &#39;&#39; - Getting the error- year = int(year_field.get()) ValueError: invalid literal for int() with base 10: '' python requests.get ValueError: int() 以 10 为基数的无效文字:'' - python requests.get ValueError: invalid literal for int() with base 10: ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM