在Python中解析大量数据时,如何处理索引超出范围错误?

问题描述 投票:0回答:2

我有一个.txt文件的大量数据,我正在尝试使用objects解析list中的Pyhon。大多数情况下,数据结构看起来都是这样,当它完成时,解析成功。

2315462;3/13/2015 8:00:00 AM;3/13/2015 1:00:00 PM
778241;1/3/2015 12:30:00 PM;1/3/2015 1:00:00 PM

如您所见,有一个id,一个开始时间和一个结束时间。它使用以下代码进行解析:

my_array_with_objects = []

with open("test.txt", newline='\n') as f:
reader = csv.reader(f, delimiter=';')

for row in reader:
    my_array_with_objects.append(Employee(row[0], row[1], row[2]))

Employee是一个看起来像这样的类:

class Employee:

def __init__(self, id, time_start, time_end):
    self.id = id
    self.time_start = time_start
    self.time_end = time_end

有时候,数据中缺少time_end

276908;1/3/20152015 8:00:00 AM

此时程序崩溃了index out of range异常。我是Python的新手,但听说没有像null这样的东西。那为什么会崩溃?我认为它可以用线上的东西来处理:

if row[2] is None:
    print("error, do things to fix")

......但它没有触发。我该如何处理这些错误?如果缺少row[2],我不希望发生任何特殊情况。空值很好。

python parsing null indexoutofboundsexception
2个回答
1
投票

你可以根据@Torxed的建议添加一个if len(row) < 3支票。更好的解决方案可能是重写Employee类并使用'splat'运算符来扩展行(列表)。对于缺失值,使用空字符串''。

这还包括缺少start_time和end_time或全部3个值的情况。

class Employee:
    def __init__(self, id='', start_time='', end_time=''):
        self.id = id
        self.start_time = start_time
        self.end_time = end_time

        # check values and convert to int, datetime...

for row in reader:
    my_array_with_objects.append(Employee(*row))

0
投票

如果你想要覆盖缺少的time_end,这应该可以解决问题:

for row in reader:
    try:
        my_array_with_objects.append(Employee(row[0], row[1], row[2]))
    except IndexError:
        my_array_with_objects.append(Employee(row[0], row[1], None))

您可以使用默认值替换None,或者选择如何在except块中处理缺少的字段

© www.soinside.com 2019 - 2024. All rights reserved.