Search code examples
pythonprintingreadfilereadlines

How to read through a file line by line and from each line selectively print the text based on the delimiters used


I have a file (application.txt) containing lines in the below format:

mytvScreen|mytvScreen|Mi TV,Mí TV
appsScreen|appsScreen|Aplicaciones,Apps
searchScreen|searchScreen|Buscar,Búsqueda
settings|settings|Configuración,Ajustes
netflix|netflix|netflix,netflis,neflix,neflis
youtube|youtube|youtube,yutub,yutiub,yutube

I need to create a python script to read the file(application.txt) line by line and from each line, it has print the first value and the values after the second delimiter ("|") .

For example:

For Line 1, the output should be in the below format.

mtvScreen : Mi Tv, Mí TV

Eg 2:

youtube :  youtube,yutub,yutiub,yutube

How can i achieve this?


Solution

  • Assumes your file is strictly adhering to the format you posted.

    with open('application.txt') as f:
        for line in f:
            parts = line.split("|")
            print(f"{parts[0]}: {parts[2]}")