How to ignore non digits in a foor loop in Python?

Viewed 156

I have a list named, new_to_csv, which looks like this:

    [['88', 'BRC', 'LON', '2020-09-07 06:05:07+00:00', '2020-09-07 09:03:00+00:00'],
    ['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00'],` 
    ['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00'],   `
    ['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00'],
    ['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00'],`       
    ['200', 'BRC', 'LON', '2020-09-07 05:08:32+00:00', '2020-09-07 13:05:00+00:00']]

I also have a file named, table_x, which looks like this:

XXXXXX  Version 1.0 BATOS DE ESPANA                                                                             
11-12-2011 t/m 08-12-2012                                                       
RG  Mini    Maxi    Organization    ISVL    PR  BSS Afw     Rage    TrT Trajecto    IBB Ant Dato    
*****data*****                                                      
R   1   99  BALEARIA    BALEARIA        Gers  INT       INT C       IB  99      
R   100 103 Espanola                    NIG             4       
R   104 105 BALEARIA    BALEARIA            PC  ICE C   Asd - Ut - Ah - Espana  IB  2       
R   106 119 Espanola                    NIG             14      
R   120 129 BALEARIA    BALEARIA            PC  ICE C   Asd - Ut - Ah - Barcelona - Almeria IB  10      
R   130 139 Espanola                    NIG             10      
R   140 149 BALEARIA    BALEARIA            PC  INT C   Shl - Amf - Dv - Hgl - Bh - Algeciras   IB  10      
R   150 159 BALEARIA    BALEARIA        SVS ICE PC  ICE C   Asd - Ut - Vl - Barcelona - Almeria IB  10      
R   160 219 BALEARIA    BALEARIA        Gere  INT   PC  INT C       IB  60      
R   220 229 BALEARIA    BALEARIA            PC  ICE C   Asd - Ut - Ah - Barcelona - Almeria IB  10      

Now for every x[0] in new_to_csv I'am first checking if its a digit or not. If it is a digit Iam checking between between what mini and maxi it occurs. I grab the columns 3:8 of the row it occurs in and paste it behind that list. If x[0] is not a digit I skip it and write 'Unkown' as output.

This is my code:

table_x = r"C:\Users\ELK\Downloads\table_x.txt"

result_list = []
with open(table_x, "r") as outer:
    reader = csv.reader(outer, delimiter="\t")
    next(reader) #skip the first 4 lines
    next(reader)
    next(reader)
    next(reader)
    for row in reader:
        mini = row[1]  
        maxi = row[2]  
        for x in new_to_csv:
            veh_number = x[0]
            check_digit = veh_number.isdigit()
            if check_digit:   #<--- check if its digit
                if int(veh_number) > int(mini) and int(veh_number) < int(maxi):
                    result_list.append(x+ [row[3]]+ [row[4]]+ [row[5]]+ [row[6]]+ [row[7]]+ [row[8]])
            if check_digit is False: #if its not digit
                result_list.append(x + [row[3]] + [row[4]] + [row[5]] + ["Unkown"])

print(result_list)

This is the output I expected:

[['88', 'BRC', 'LON', '2020-09-07 06:05:07+00:00', '2020-09-07 09:03:00+00:00', 'BALEARIA', 'BALEARIA', '', 'Gers  INT', '', 'INT'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00','Unkown', 'Unkown', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'Unkown', 'Unkown', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00','2020-09-07 23:30:01+00:00', 'Unkown', 'Unkown', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'Unkown', 'Unkown', '', 'Unkown'],
['200', 'BRC', 'LON', '2020-09-07 05:08:32+00:00', '2020-09-07 13:05:00+00:00', 'BALEARIA', 'BALEARIA', '', 'Gere  INT', 'PC', 'INT']]

Above you see the output I expected. 88 is a digit and is between 1 and 99 so I grab the columns 3:8 from that row. *021 is not a digit so I write 'Unknown' etc..

Now below you see the output I received, it seems to do the non-digits multiple times. I dont understand why...

This is the output I received:

[['88', 'BRC', 'LON', '2020-09-07 06:05:07+00:00', '2020-09-07 09:03:00+00:00', 'BALEARIA', 'BALEARIA', '', 'Gers  INT', '', 'INT'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00','BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00','2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'Espanola', '', '', 'Unkown'],
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'Espanola', '', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'Espanola', '', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'Espanola', '', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
 ['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
 ['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'Espanola', '', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'Espanola', '', '', 'Unkown'],
 ['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'Espanola', '', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'Espanola', '', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'Espanola', '', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'Espanola', '', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'Espanola', '', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'Espanola', '', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
[ 'nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA','BALEARIA', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'],
['200', 'BRC', 'LON', '2020-09-07 05:08:32+00:00', '2020-09-07 13:05:00+00:00', 'BALEARIA', 'BALEARIA', '', 'Gere  INT', 'PC', 'INT'], 
['*021', 'BRC', 'LON', '2020-09-07 08:26:07+00:00', '2020-09-07 09:34:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['*023', 'BRC', 'LON', '2020-09-07 08:30:00+00:00', '2020-09-07 13:50:18+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['nodigit', 'BRC', 'LON', '2020-09-07 03:00:03+00:00', '2020-09-07 23:30:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown'], 
['++158', 'BRC', 'LON', '2020-09-07 05:42:05+00:00', '2020-09-07 10:02:01+00:00', 'BALEARIA', 'BALEARIA', '', 'Unkown']]
 
1 Answers

Change your line to it.

if check_digit is False: #if its not digit
      result_list.append(x + ["Unkown"] + ["Unkown"] + [""] + ["Unkown"])

You were adding [row[3]] + [row[4]] + [row[5]] to x

***** FULL CODE *****

table_x = r"table_x.txt"

result_list = []
with open(table_x, "r") as outer:
    reader = csv.reader(outer, delimiter="\t")
    next(reader) #skip the first 4 lines
    next(reader)
    next(reader)
    next(reader)
    for row in reader:
        mini = row[1]  
        maxi = row[2]


        for x in new_to_csv:
            veh_number = x[0]
            check_digit = veh_number.isdigit()
            if check_digit:   #<--- check if its digit
                if int(veh_number) > int(mini) and int(veh_number) < int(maxi):
                    result_list.append(x+ [row[3]]+ [row[4]]+ [row[5]]+ [row[6]]+ [row[7]]+ [row[8]])

    for x in new_to_csv:
        veh_number = x[0]
        check_digit = veh_number.isdigit()
        if check_digit is False: #if its not digit
            result_list.append(x + ["Unkown"] + ["Unkown"] + [""] + ["Unkown"])
print(result_list)
Related