1

I'm reading a file with a variable number of columns, say 3 fixed columns + unknown/variable number of columns:

21 48 77
15 33 15 K12
78 91 17
64 58 24 R4 C16 R8
12 45 78 Y66
87 24 25
10 33 75
18 19 64 CF D93

I want to store the first three column entries in specific lists/arrays, because I need to work with them, while putting all the remaining part of the line (from column[2] to the end of line) in another single string, as I don't need to act on it, but just to copy it.

I wrote:

import os, sys
import numpy as np

fi = open("input.dat", "r")
fo = open("output.dat", "w")

for line in fi:
    line = line.strip()
    columns = line.split()
    A00 = str(columns[0])
    A01 = str(columns[1])
    A02 = str(columns[2])
    A03 = EVERTHING ELSE UNTIL END OF LINE

Is there an easy way to do this? Thanks in advance.

urgeo
  • 645
  • 1
  • 9
  • 19

3 Answers3

1

You can use this code :

import os, sys
import numpy as np
fi = open("input.dat", "r")
fo = open("output.dat", "w")
column_3 = []

for line in fi:
    line = line.strip()
    columns = line.split()
    A00 = str(columns[0])
    A01 = str(columns[1])
    A02 = str(columns[2])
    column_3.append(str(columns[3]))
print(column_3)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mikail Land
  • 269
  • 3
  • 9
1

String split allows to limit number of extracted parts, so you can do following:

A00, A01, A02, rest = line.split(" ", 3)

Example:

print "1 2 3 4 5 6".split(" ", 3)
['1', '2', '3', '4 5 6']
Mirec Miskuf
  • 1,695
  • 1
  • 14
  • 13
  • alternatively, you could also do: `A00, A01, A02, *rest = line.split()` which would turn `rest` into a list like so : `rest = ['4', '5', '6']`. Note that `rest` would still be a list even if the remaining element was a single one (for example `rest = ['4']`) – Ma0 Sep 13 '16 at 12:41
0

I think following snippet code can help you. Also, you can edit this code for your project.

f = open("input.dat") 

line = f.readline().strip() # get the first line in line

while line: # while a line exists in the file f
    columns = line.split('separator') # get all the columns
    while columns: # while a column exists in the line
        print columns # print the column
    line = f.readline().strip() # get the next line if it exists

I hope it helps you.

Ashouri
  • 906
  • 4
  • 19