1

I'm a newbie in Python and I've a problem.

I've to compare the values between two files, one is an Excel file (and I don't have any problem with it), the other one is a text file formatted with spaces and "blocks" of lines.

The text file is like this:

LISON                 Kontoauszug                          10.07.2016           20:13
                     Monat/Jahr: 06.16                               Seite:     1

Lief. : AKJsjak0  (V Sachbearb.: Name    Surname
LT    : VW0012    Lief.-Eigene.:    0                  Tel.: xxxxxxxxxx

Saldo Vorm.:     170  BEL.:     253 ENTL:     181 Endsaldo:      242

B-Dat  Abs/Empfae   BEL.    ENTL   Saldo  BA  Bel-Nr    WK-LG-LR   Bemerkung
050416 000590178       0       1     169  50  16103483  49-12-00   FERSR IM SY
050416 000590178       0       1     168  50  16103484  49-16-00   FERSR IM SY
050516 000590030       0       2     166  50  16104633  16-01-K1
160516 000590030       0       1     165  50  16104980  16-01-K1
170516 000590030       0       2     163  50  16105015  16-01-K1
210516 000590120       1       0     164  51     36873  37-  -        000590120
230516 000590178       1       0     165  51  16105229  49-16-00   MPYTRRIN
240516 000590030       0       2     163  50  16105243  16-01-K1
300516 000590030       0       1     162  50  16105484  16-01-K1
300516 000590030       0       1     161  50  16105483  16-01-K1
310516 000590030       2       0     163  51    697321  26-  -     KOR.GJKE.MB
310516 000590030       0       2     161  50  16105536  16-01-K1
310516 000590030       0       1     160  50  16105542  16-01-K1
010616 000590120       2       0     162  21     39694  37-  -        000590120
010616 000710030      12       0     174  21    627948  21-  -       000710030
010616 000590120       0       1     173  50     39694  37-  -
030616 000712550       0       2     171  10  16105627  28-05-60
030616 000710130       0       1     170  10  16105628  11-01-K4
030616 000448489       0       2     168  10  16105638  18-66-23
030616 000590120       0       2     166  10  16105626  37-75-I4
060616 000590030      41       0     207  21    698299  26-  -         000590030
070616 000712550       0       2     205  10  16105714  28-05-60
070616 000712550       0       1     204  10  16105717  28-08-60
070616 000590178       0       1     203  10  16105710  49-16-
070616 000590120       0       1     202  10  16105702  37-75-I4
070616 000590120       0       1     201  10  16105703  37-78-I4
070616 000590120       0       1     200  10  16105704  37-78-I8
070616 000590235       0       1     199  10  16105707  33-07-K9
070616 000710030       0       1     198  10  16105715  24-06-S2
070616 000590030       0       1     197  10  16105716  16-01-K1
070616 000590030       0       1     196  10  16105722  16-01-K1
070616 000590030       0       3     193  10  16105726  16-01-K1
070616 000711420       0       1     192  10  16105706  40-01-K1
080616 000590120       1       0     193  21     31456  37-  -       000590120
080616 000590120       1       0     194  21     31456  37-  -       000590120
080616 000710030       2       0     196  21    630076  21-  -     000710030
080616 000710030       2       0     198  21    630076  21-  -     000710030
080616 000710030       4       0     202  21    630076  21-  -        000710030
080616 000710136       0       1     201  10  16105769  15-01-F4
090616 000590178       2       0     203  21    491379  49-  -       000590178
090616 000710030       0       1     202  10  16105842  21-01-P0
090616 000710030       0       4     198  10  16105843  21-01-P0
-------------------------------------------------------------------------------
-                                                              -
BA=10 Entlast. durch Lieferschein       BA=11 Belast. durch Lieferschein
BA=20 Entlast. durch PV-Schein          BA=21 Belast. durch PV-Schein
BA=22 Entlast. durch MRV-/Lieferschein  BA=23 Belast. durch MRV-  /Lieferschein
BA=30 Entlast. durch Querverkehr        BA=31 Belast. durch Querverkehr
BA=50 Entlast. durch Korrektur          BA=51 Belast. durch Korrektur
BA=70 Entlast. durch Inventurangleich   BA=71 Belast. durch   Inventurangleich
BA=NE Entlast. durch NeG neutr. Buch.   BA=NB Belast. durch NeG neutr.   Buch.
BA=NK Neukauf                           BA=VS Verschrottung
BA=NW Neukauf Wertersatz                BA=NR Neukauf Recycling
BA=VR Verschrottung Recycling





LISON                 Kontoauszug                          10.07.2016        20:13
                     Monat/Jahr: 06.16                              Seite:   2

And so on for thousands of lines... I need a list(maybe?) or a np.array (if it's better) that is made of every columns of the txt (B-Dat Abs/Empfae BEL etc.) + the LT code of every line.

The TXT is like "For this LT code, these movement are made... In this day, with this ID, this quantity are gone"

I just write this code, but I don't know how to procede... I've tried both np and also standard modules...

with open("elementi/june/VW.txt", "r") as ins:
testo_VW = []  # lines length 77 chars
for line in ins:
    testo_VW.append(line)  


codiceCasse = [
    "VW0012",
    "001210",
    "114003",
    "004147",
    "151774",
    "151743",
    "511912",
    "525411",
    "528879",
    "006280"
]

indiciVW = []
codcasseVW = []
b_dat = []  # array di date prese dal file VW
abs_empfae = []
bel = []
entl = []
bel_nr = []
wk_lg_lr = []

count = 0
for i in range(0, len(testo_VW)):
    if any(x in testo_VW[i] for x in codiceCasse):  # ----> trovo i numeri delle casse nel txt
        riga = testo_VW[i]
        codice_cassa = riga[8:14]
        i += 6
        count += 1

Can you please give me any advice? Or maybe something for implementing the code...

Thank you in advance.

1
  • Read the file line by line using readln(), then you will get a string split it and get the data. Commented Jul 27, 2016 at 10:51

1 Answer 1

1

One approach would be to read the file in a line at a time and use a regular expression to decide if it is one of the data lines. If it is, append to a list. You will also need to keep a note of the LT line and append it to any following data lines as follows:

import re

data = []
lt = 'unknown'

with open('input.txt') as f_input:
    for row in f_input:
        data_row = re.match(r'(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(\d+) +(.{8}) +(.*)|LT +: (\w+)', row)

        if data_row:
            if data_row.groups()[0]:
                data.append([lt] + list(data_row.groups()[:-1]))
            else:
                lt = data_row.groups()[-1]

print data    

This would give you the following to work with:

[['VW0012', '050416', '000590178', '0', '1', '169', '50', '16103483', '49-12-00', 'FERSR IM SY'], ['VW0012', '050416', '000590178', '0', '1', '168', '50', '16103484', '49-16-00', 'FERSR IM SY'], ['VW0012', '210516', '000590120', '1', '0', '164', '51', '36873', '37-  -  ', '000590120'], ['VW0012', '230516', '000590178', '1', '0', '165', '51', '16105229', '49-16-00', 'MPYTRRIN'], ['VW0012', '310516', '000590030', '2', '0', '163', '51', '697321', '26-  -  ', 'KOR.GJKE.MB'], ['VW0012', '010616', '000590120', '2', '0', '162', '21', '39694', '37-  -  ', '000590120'], ['VW0012', '010616', '000710030', '12', '0', '174', '21', '627948', '21-  -  ', '000710030'], ['VW0012', '060616', '000590030', '41', '0', '207', '21', '698299', '26-  -  ', '000590030'], ['VW0012', '080616', '000590120', '1', '0', '193', '21', '31456', '37-  -  ', '000590120'], ['VW0012', '080616', '000590120', '1', '0', '194', '21', '31456', '37-  -  ', '000590120'], ['VW0012', '080616', '000710030', '2', '0', '196', '21', '630076', '21-  -  ', '000710030'], ['VW0012', '080616', '000710030', '2', '0', '198', '21', '630076', '21-  -  ', '000710030'], ['VW0012', '080616', '000710030', '4', '0', '202', '21', '630076', '21-  -  ', '000710030'], ['VW0012', '090616', '000590178', '2', '0', '203', '21', '491379', '49-  -  ', '000590178']]    
7
  • That's awesome, thank you! That's what's I'm searching... Can you please explain me, Ever if you want, what that Regex do?
    – andrepogg
    Commented Jul 27, 2016 at 12:01
  • RegEx is a huge topic you would be wise to learn. In effect each section in parenthesis is something wanted, + means 'one or more' and \d+ means one or more digits. At the end is | meaning or. So it either matches your data rows, or it matches the LT line. I would suggest you print data.row.groups() to see what is obtained for each line, it would then make sense of the rest of the code. Commented Jul 27, 2016 at 12:04
  • Very appreciated, you solved my problem!!! I today I will read also the pyDoc for learn more about RegEx...
    – andrepogg
    Commented Jul 27, 2016 at 12:14
  • You are welcome. If you are happy with the solution, don't forget to click the grey tick next to the answer to accept the solution. Commented Jul 27, 2016 at 12:15
  • I've clicked, but StackOverflow doesn't show my selection! But it says that the vote was registered!
    – andrepogg
    Commented Jul 27, 2016 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.