-2

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Kiran Das
  • 57
  • 6

2 Answers2

2

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]}")
rdas
  • 20,604
  • 6
  • 33
  • 46
0

Just a change to whatever @rdas answered earlier, in order to print everything that's there after the second delimiter, so would be changing the split method to add the number of splits that need to be done.

with open('application.txt') as f:
    for line in f:
      details= line.split("|",2)
      print(details[0] + ' : '+ details[2])

Hope it helps. Happy Coding. !

Strik3r
  • 1,052
  • 8
  • 15