How do I read two columns of a . dat file in Python?

Asked

Viewed 2,014 times

1

I have a file . dat with the following numbers:

34875    70.9300    -8.6700

54872    80.0500    16.2500

65456    77.0000    -5.5000

78787    78.2500    22.8300

20890    -8.9200    11.9300

87859    78.2500    15.4700

89556    80.6700    25.0000

I wanted to know how I would read only the second and third column using Python.

2 answers

1


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 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

  • Dry would be "fewer lines of code". Yes, but your response also helped.

0

Short solution:

with open('arq.txt') as arq:
    dados = [line.split('    ')[1:] for line in arq.readlines() if line.strip()]
print(dados)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.