我正在阅读Python中的CSV文件,如下所示:
REGION,1910,1920,1930,1940,1950,1960,1970,1980,1990,2000,2010
Alabama,2138093,2348174,2646248,2832961,3061743,3266740,3444165,3893888,4040587,4447100,4779736
Alaska,64356,55036,59278,72524,128643,226167,300382,401851,550043,626932,710231
我的问题是,当我读到第一行时,它将其读作
REGION,1910,1920,1930,1940,1950,1960,1970,1980,1990,2000,2010
首先并不像一个问题。 但是后来我找了一个数字,所以将字符串拆分成一个列表
lijst_eerste_regel = self.eerste_regel.split(",")
然后寻找str(2010)的索引,但Python似乎在寻找' 2010'不是" 2010"。因此,它找不到索引。
我在这里发布了代码(它是在一个类我遇到这个问题,不确定这是否相关)
import io
class Volkstelling:
def __init__(self,jaartal,csvb):
"""
>>> vs2010 = Volkstelling(2010, 'vs_bevolkingsaantal.csv')
"""
import csv
self.jaartal = jaartal
self.csvb = csvb
self.eerste_regel = next(self.csvb)
if str(jaartal) not in self.eerste_regel:
raise AssertionError ("geen gegevens beschikbaar")
def inwoners(self, regio):
lijst_eerste_regel = self.eerste_regel.split(",")
plaats_jaartal = lijst_eerste_regel.index(self.jaartal) # here is where the error occurs
data = """REGION,1910,1920,1930,1940,1950,1960,1970,1980,1990,2000,2010
Alabama,2138093,2348174,2646248,2832961,3061743,3266740,3444165,3893888,4040587,4447100,4779736
Alaska,64356,55036,59278,72524,128643,226167,300382,401851,550043,626932,710231"""
v = Volkstelling('2010',io.StringIO(data))
v.inwoners('Alabama')
## ValueError: '2010' not in list
答案 0 :(得分:0)
您的代码存在导致2010年未找到的几个问题:
如果您在文件中读取,则每行最后都有一个newline character,通常表示为\n
。将以下代码插入inwoners
函数,以查看2010年后面的换行符:
print(lijst_eerste_regel)
您可以使用python函数'SOME STRING'.strip()
您的函数未返回值,因此即使None
正确运行,您也会从inwoners
获得import io
class Volkstelling:
def __init__(self,jaartal,csvb):
"""
>>> vs2010 = Volkstelling(2010, 'vs_bevolkingsaantal.csv')
"""
import csv
self.jaartal = jaartal
self.csvb = csvb
self.eerste_regel = next(self.csvb)
if str(jaartal) not in self.eerste_regel:
raise AssertionError ("geen gegevens beschikbaar")
def inwoners(self, regio):
lijst_eerste_regel = [s.strip() for s in self.eerste_regel.split(",")]
plaats_jaartal = lijst_eerste_regel.index(self.jaartal)
return plaats_jaartal # Returns the column index where to find the no of inhabitants
data = """REGION,1910,1920,1930,1940,1950,1960,1970,1980,1990,2000,2010
Alabama,2138093,2348174,2646248,2832961,3061743,3266740,3444165,3893888,4040587,4447100,4779736
Alaska,64356,55036,59278,72524,128643,226167,300382,401851,550043,626932,710231"""
v2 = Volkstelling('1920',io.StringIO(data))
print(v2.inwoners('Alabama'))
## -> prints 2
v1 = Volkstelling('2010',io.StringIO(data))
print(v1.inwoners('Alabama'))
## -> prints 11
。
以下示例有效:
constructor(props){
super(props);
this.sortIT = this.sortIT.bind(this);
this.state = { isHide : false, isResult:[], isShow : true};
}