1

I have a data file with two columns and I wanna put the into a singe column.

Since now I have splitted the columns with open("test.txt") as input_data:

for line in input_data:  # This keeps reading the file
    li=line.strip()
    #print repr(line) #Each line is being returned as a string
    column = line.split()
    # print column
    nb1=column[0]
    nb2=column[1]

How can I continue?

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Do you want one column on top of the other column? Do you want them weaved together where the second element on each line is directly below the first element? – Octipi Feb 25 '13 at 10:24
  • i have two columns and I want after the last number of the first column the second column to be added. So I need all the numbers to be in a singe column. For example if i have two columns with ten numbers each, I need a column with twenty numbers as output – user2106755 Feb 25 '13 at 10:39
  • Please post a clear example of input and expected output in your answer. – Adam Matan Feb 25 '13 at 10:48
  • i have "1 10" and i want to put 10 under 1. (Sorry but I really can't find out how to use Markdown formatting in order to explain better) – user2106755 Feb 25 '13 at 10:57

2 Answers2

1

You could use Python's slicing notation combined with the + operator on your columns. For example, joining the first two elements of a list is done by:

=>>> l=["a","b","c","d"]
>>> a=[l[0]+l[1]]+l[2:]
>>> a
['ab', 'c', 'd']

a=[l[0]+l[1]]+l[2:] joins (+) the first two elements ([l[0]+l[1]]) with the remaining columns (l[2:]).

In your code, it would probably be:

columns = [column[0]+ column[1]]+ column[2:]
Community
  • 1
  • 1
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • actually what i need (sorry for not making it clear) is to put b under a (in your example) so to make a column with two elements a,b – user2106755 Feb 25 '13 at 10:45
0

I changed nb1 and nb2 into lists and appended the column values to each adding '\n' so they are written on a new line. The output file has a single column with the values from column 1 on top of the values from column 2. This approach works well for the most part but if you are dealing with really large files this method uses a lot of memory. If that is an issue you can form your output file in two passes writing first the first column to file and then the second column.

nb1 = []
nb2 = []
with open('input.txt','r') as input_data:
  for line in input_data:
    column = line.strip().split()
    nb1.append(column[0]+'\n')
    if len(column) == 2:
      nb2.append(column[1]+'\n')
with open('output.txt','w') as f:
  f.writelines(nb1)
  f.writelines(nb2)

If you wanted the second element in each row under the first element you could use this simpler more memory friendly syntax.

with open('input.txt','r') as input_data:
  with open('output.txt','w') as f:
    for line in input_data:
      column = line.strip().split()
      f.write(column[0]+'\n')
      f.write(column[1]+'\n')
Octipi
  • 835
  • 7
  • 12
  • thanks a lot for you answer. But what if in column a I have e.g 10 numbers and I want to add after those 10 numbers the numbers of column b (e.g 5 numbers) in order to have a single column c with 15 numbers??? – user2106755 Feb 25 '13 at 13:01
  • I updated my answer to cover this. Just check the size of column and if it has two elements then append the second element to nb2. – Octipi Feb 25 '13 at 13:08