To csv file
Since you are open to other languages, I offer a solution in Python. It creates a csv file that looks like this:
"AC","Acrelândia","TV","Canal 18","AINDA NĂO OUTORGADO" "AC","Acrelândia","RTV","Canal 9","RADIO TV DO AMAZONAS LTDA" "AC","Acrelândia","RTV","Canal 10","RADIO TV DO AMAZONAS LTDA(REDENCAO)" "AC","Acrelândia","TVD","Canal 15","RADIO TV DO AMAZONAS LTDA" "AC","Acrelândia","TVD","Canal 15","AINDA NĂO OUTORGADO(REDENĂĂO)" "AC","Acrelândia","FM","88,5 MHz","RADIO E TV MAIRA LTDA" "AC","Assis Brasil","TV","Canal 34","AINDA NĂO OUTORGADO" "AC","Assis Brasil","RTV","Canal 6","AMAZONIA CABO LTDA" "AC","Assis Brasil","RTV","Canal 10","RADIO TV DO AMAZONAS LTDA" "AC","Assis Brasil","RTV","Canal 13","AINDA NĂO OUTORGADO" "AC","Assis Brasil","RTV","Canal 45","FUNDACAO JOAO PAULO II"
The code
This makes two assumptions: (1) The first line in the file or any line following an empty line is the name of the station and (2) The fields are separated by two or more spaces
#-*- coding: utf-8 -*- import re import csv # CREATE DATA STRUCTURE TO SIMULATE READING A TEXT FILE data = u'''AC-Acrelândia TV Canal 18 AINDA NĂO OUTORGADO RTV Canal 9 RADIO TV DO AMAZONAS LTDA RTV Canal 10 RADIO TV DO AMAZONAS LTDA(REDENCAO) TVD Canal 15 RADIO TV DO AMAZONAS LTDA TVD Canal 15 AINDA NĂO OUTORGADO(REDENĂĂO) FM 88,5 MHz RADIO E TV MAIRA LTDA AC-Assis Brasil TV Canal 34 AINDA NĂO OUTORGADO RTV Canal 6 AMAZONIA CABO LTDA RTV Canal 10 RADIO TV DO AMAZONAS LTDA RTV Canal 13 AINDA NĂO OUTORGADO RTV Canal 45 FUNDACAO JOAO PAULO II'''.split('\n') def read_records(): for line in data: yield line # INITIALIZE SPLITTER, READ RECORDS AND WRITE TO CSV FILE splitter = re.compile('\s{2,}') change_station = True station = '' f = open('./output.csv', 'w') writer = csv.writer(f, quoting=csv.QUOTE_ALL) for rec in read_records(): rec = rec.strip() if rec == '': change_station = True elif change_station == True: station = rec.replace('-', ' ') change_station = False else: record = station + ' ' + rec record = record.encode('utf-8') record = re.split(splitter, record) writer.writerow(record) f.close() # READ IN FILE AND PRINT TO CONSOLE FOR DEMO PURPOSES f = open('./output.csv', 'r') print ''.join( f.readlines() ) f.close()