krizz
krizz

Reputation: 125

Python - Error while trying to split line of text

I am having as issue while trying to split a line of text I get from .txt file. It is quite a big file, but I will paste only 2 lines, with original text

1307;Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode;KS1J/00080000/2;861;Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode;KS1J/00080990/2;

1306;Własność: udział 1/1<>Jan Nowak<>im. rodz.: Tomasz_ Maria<>Somewhere 2<>30-200 ZipCode;KW22222;861;Własność: udział 1/1<>GMINA TARNOWIEC<><>Tarnowiec 211<>30-200 ZipCode;KS1W/00080000/1;

Data I get from this file will be used to create reports, and _ and <> will be used for further formatting. I want to have the line split on ;

Problem is, I am getting error on 2 methods of splitting.

first, the basic .split(';')

dane = open('dane_protokoly.txt', 'r')
for line in dane:
    a,b,c,d,e,f,g = line.split(';')
    print(a)
    print(b)
    print(c)
    print(d)
    print(e)
    print(f)
    print(g)

I am getting an error after printing the first loop

Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Nowy folder\costam.py", line 36, in <module>
a,b,c,d,e,f,g = line.split(';')
ValueError: not enough values to unpack (expected 7, got 1)

Same with creating lists from this file (list looks like: ['1307', 'Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode', 'KS1J/00080000/2', '861', 'Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode', 'KS1J/00080990/2', '']

dane = plik('dane_protokoly.txt')
for line in dane:
    a = line[0]
    b = line[1]
    c = line[2]
    d = line[3]
    e = line[4]
    f = line[5]
    g = line[6]
    print(str(a))
    print(str(b))
    print(str(c))
    print(str(d))
    print(str(e))
    print(str(f))

error I get also after properly printing the first line:

Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\Nowy folder\costam.py", line 22, in <module>
    b = line[1]
IndexError: list index out of range

Any idea why am I getting such errors?

Upvotes: 0

Views: 1933

Answers (3)

Christian K&#246;nig
Christian K&#246;nig

Reputation: 3570

As Rahul K P mentioned, the problems are the "empty" lines in between your lines with the data. You should skip them when trying to split your data.

Maybe use this as a starting point:

with open(r"dane_protokoly.txt", "r") as data_file:
    for line in data_file:
        #skip rows which only contain a newline special char
        if len(line)>1:
            data_row=line.strip().split(";")
            print(data_row)

Upvotes: 1

Rahul K P
Rahul K P

Reputation: 16081

Sometimes line.split(';') not giving 7 values to unpack for (a,b,c,...), So better to iterate like this ,

lst = line.split(';')
for item in lst:
    print item

And there is a newline in between that's making the problems for you, And the syntax that followed is a bad practice

You change your code like this,

for line in open("'dane_protokoly.txt'").read().split('\n'):
    lst = line.split(';')
    for item in lst:
        print item

It's doesn't care about the newlines in between,

Upvotes: 3

Elodin
Elodin

Reputation: 648

Your second strategy didn't work because line[0] is essentially the whole line as it includes no spaces and the default is splitting at spaces. Therefore there is no line[1] or line[2]... and therefore you get a list index out of range error.

I hope this helps. And I hope it solves your problem.

Upvotes: 0

Related Questions