Search code examples
pythontextdata-extraction

Extracting information from txt file using python


I have a TXT file that looks like this

ETP   474654 0|170122|160222|MXP|    14045.84|           |     4711.00|       0|      0|      0|      0|   4711|      0
BA6 91215257 1|310122|      |MXP|            |    9053.93|            |        |       |       |       |       |
TDO   301530 1|010222|      |MXP|            |     280.91|            |        |       |       |       |       |
ETP   475384 0|260122|250222|MXP|   198340.87|           |      917.70|       0|      0|      0|      0|    917|      0
ANC 91163164 2|290122|      |MXP|            |     200.66|            |        |       |       |       |       |
BA6 91215555 1|140222|      |MXP|            |  193278.06|            |        |       |       |       |       |
TDO   302435 1|150222|      |MXP|            |    3944.45|            |        |       |       |       |       |
ETP   481186 0|020422|020522|MXP|    53597.34|           |      184.08|       0|      0|    184|      0|      0|      0
ANC 91164671 2|120422|      |MXP|            |     324.32|            |        |       |       |       |       |
BA6 91217161 1|200422|      |MXP|            |   52027.16|            |        |       |       |       |       |
TDO   306773 1|210422|      |MXP|            |    1061.78|            |        |       |       |       |       |
ETP   481188 0|020422|020522|MXP|    82599.09|           |      275.29|       0|      0|    275|      0|      0|      0
BA6 91217159 1|200422|      |MXP|            |   80677.32|            |        |       |       |       |       |
TDO   306775 1|210422|      |MXP|            |    1646.48|            |        |       |       |       |       |
ETP   483241 0|020522|010622|MXP|   162587.22|           |    20367.05|       0|  20367|      0|      0|      0|      0
ANC 91165149 2|060522|      |MXP|            |    1930.81|            |        |       |       |       |       |
BA6 91217906 2|230522|      |MXP|            |  137083.58|            |        |       |       |       |       |
TDO   308497 1|240522|      |MXP|            |    3205.78|            |        |       |       |       |       |
ETP   485561 0|300522|290622|MXP|    43411.90|           |    43181.22|   43181|      0|      0|      0|      0|      0
ANC 91165759 2|020622|      |MXP|            |     230.68|            |        |       | 

I want to extract all of the data in each row that contains ETP.
The first 6 digit number ist the ETP ID.
The number |170122| is a date.
The number |160222| is a date.
The next Value |14045.84| value should also be displayed.
If there is a Non 0 value, in this case the Next non 0 value is |4711.00| it should also be displayed.

It should return something like

ETP 474654 | 170122 | 160222 | 14045.84 | 4711.00  

IDEALLY it should format the date and look like this

ETP 474654 | 17/01/22 | 16/02/22 | 14045.84 | 4711.00  

I am new to python and would like to know if this is possible and if someone could point me in the right direccion to solve this. Thanks for the help.


Solution

  • Thanks for the answers! Unfourtunatly, as I said I am new to Python and I am using a web compiler. Fourtunatly I was able to find a simpler solution, I am a bit surprised no one suggested it but then again, maybe you are also new to python.

    file = open("data.txt", 'r')
    word = input("Escribe ETP y presiona enter:")
    s=" "
    count=1
    
    while(s):
     s=file.readline()
     L=s.split()
     if word in L:
             print("Linea:", count, ":",s)
     count+=1