-1

I have a text file composed mostly of numbers something like this:

3  011236547892X
9  02321489764 Q
4  031246547873B

I would like to extract each of the following (spaces 5 to 14 (counting from zero)) into a list:

1236547892
321489764 
1246547873

(Please note: each "number" is 10 "characters" long - the second row has a space at the end.)

and then perform analysis on the contents of each list.

I have umpteen versions, however I think I am closest with:

with open('k_d_m.txt') as f:
        for line in f:
            range = line.split()
            num_lst = [x for x in range(3,10)]

print(num_lst)

However I have: TypeError: 'list' object is not callable

What is the best way forward?

What I want to do with num_lst is, amongst other things, as follows:

num_lst = list(map(int, str(num)))

print(num_lst)

nth = 2

odd_total = sum(num_lst[0::nth]) even_total = sum(num_lst[1::nth]) print(odd_total) print(even_total)

if odd_total - even_total == 0 or odd_total - even_total == 11: print("The number is ok") else: print("The number is not ok")

Roderic
  • 63
  • 1
  • 7

2 Answers2

1

Use a simple slice:

with open('k_d_m.txt') as f:
    num_lst = [x[5:15] for x in f]

Response to comment:

with open('k_d_m.txt') as f:
    for line in f:
        num_lst = list(line[5:15])
        print(num_lst)
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Hi Mozway, unfortunately all the numbers are included in one list. I need each number and only that number to be in num_lst. I then perform analysis on the number and then do the next one – Roderic Nov 24 '21 at 22:06
  • Can you edit your question to provide the expected output (as a valid python object)? – mozway Nov 24 '21 at 22:08
  • Hi Mozway, after reading a row I would like to have a list like this: num_lst = [1, 2, 3, 6, 5, 4, 7, 8, 9, 2] – Roderic Nov 24 '21 at 22:19
  • This I use as input into the following: num_lst = list(map(int, str(num))) print(num_lst) nth = 2 odd_total = sum(num_lst[0::nth]) even_total = sum(num_lst[1::nth]) print(odd_total) print(even_total) if odd_total - even_total == 0 or odd_total - even_total == 11: print("The number is ok") else: print("The number is not ok") – Roderic Nov 24 '21 at 22:20
  • Comments are not good to post multi-line code, please edit the question. Anyway, check my update for the ground principle – mozway Nov 24 '21 at 22:23
  • Sorry Mozway, I don't seem to have been able to get the code formatted nicely – Roderic Nov 24 '21 at 22:24
0

First of all, you shouldn't name your variable range, because that is already taken for the range() function. You can easily get the 5 to 14th chars of a string using string[5:15]. Try this:

num_lst = []
with open('k_d_m.txt') as f:
    for line in f:
        num_lst.append(line[5:15])
print(num_lst)
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23
  • Hi Erik, unfortunately all the numbers are included in one list. I need each number and only that number to be in num_lst. I then perform analysis on the number / num_lst and then do the next one – Roderic Nov 24 '21 at 22:06