You didn’t specify the final format that it should have, but I’m going to assume that it’s a list of several sub-lists, each of which has the second and third column data.
You can do it this way:
data = []
with open('tests.dat') as f: # abrir ficheiro em modo leitura ('r'), default, equivalente a ...open('tests.dat', 'r')...
for line in f:
if(line.strip() != ''): # exitem linhas vazias, vamos ignora-las
cols = [i.strip() for i in line.split(' ')[1:]] # vamos quebrar cada linha por espacos e ignorar o primeiro valor de cada linha
data.append(cols) # adcionar as cols desejadas a nossa lista principal
print(data) # [['70.9300', '-8.6700'], ['80.0500', '16.2500'], ['77.0000', '-5.5000'], ['78.2500', '22.8300'], ['-8.9200', '11.9300'], ['78.2500', '15.4700'], ['80.6700', '25.0000']]
More didactic way:
data = []
with open('tests.dat') as f:
for line in f:
if(line.strip() != ''): # exitem linhas vazias, vamos ignora-las, nao fazer nada com elas
line_spl = line.split(' ') # vamos quebrar cada linha por espacos
cols = line_spl[1:] # aproveitar o segundo e terceiro elemento da linha quebrada por espacos
data.append([cols[0], cols[1].strip()]) # retirar '\n' do terceiro elemento, segundo em cols
print(data) # [['70.9300', '-8.6700'], ['80.0500', '16.2500'], ['77.0000', '-5.5000'], ['78.2500', '22.8300'], ['-8.9200', '11.9300'], ['78.2500', '15.4700'], ['80.6700', '25.0000']]
You can print every cycle, every line, every cols
desired:
with open('tests.dat') as f:
for line in f:
if(line.strip() != ''):
line_spl = line.split(' ')
cols = [i.strip() for i in line.split(' ')[1:]]
print(cols[0], cols[1].strip())
Oops!! Thank you for the reply Miguel! so that way the script would read without using any library, right? But I used the Pandas to read, and became more "dry" the code.
– b3g1nn3er
@b3g1nn3er wiped down? What’s that? Yeah, I didn’t know it was with pandas, you’d usually specify that in the question. Nothing, good luck
– Miguel
Dry would be "fewer lines of code". Yes, but your response also helped.
– b3g1nn3er