Concatenating two text files in Python

I am a very Python beginner. Therefore, my question may be quite naive. I just started learning this language mainly because of math tools like Numpy and Matplotlib which seem to be very useful.

I don't actually see how python works in non-math fields I wonder if it is possible (and if so how?) To use Python for problems like handling text files.

Or rather, you can solve this problem:

I have two files A.txt and B.txt. The A.txt file contains three columns of numbers and looks like this:

 0.22222000  0.11111000  0.00000000   
 0.22222000  0.44444000  0.00000000   
 0.22222000  0.77778000  0.00000000   
 0.55556000  0.11111000  0.00000000   
 0.55556000  0.44444000  0.00000000   
.....

      

The B.txt file contains three columns of the letters F or T and looks like this:

  F   F   F   
  F   F   F   
  F   F   F   
  F   F   F   
  T   T   F   
......

      

The number of lines is the same in files A.txt and B.txt

I need to create a file that looks like

   0.22222000  0.11111000  0.00000000   F   F   F   
   0.22222000  0.44444000  0.00000000   F   F   F   
   0.22222000  0.77778000  0.00000000   F   F   F   
   0.55556000  0.11111000  0.00000000   F   F   F  
   0.55556000  0.44444000  0.00000000   T   T   F 

      

.......

In other words, I need to create a file that contains 3 columns A.txt and then 3 columns of B.txt file.

Can anyone help me write the lines in python required for this?

I could easily do this in fortran, but heard that the script in python would be much smaller. And since I started learning math tools in Python, I also want to expand my knowledge to other possibilities offered by this language.

Thank you in advance

+3


source to share


3 answers


Of course Python can be used for text processing (it may even be better suited for this than for numeric assignments). However, this task can be accomplished with a single Unix command:paste A.txt B.txt > output.txt

And here is a Python solution without using numpy

:



 with open('A.txt') as a:
     with open('B.txt') as b:
         with open('output.txt', 'w') as c:
             for line_a, line_b in zip(a, b):
                 c.write(line_a.rstrip() + ' ' + line_b)

      

+4


source


If you want to compose them in a good way and put them in a new file, you can do this:



a = open('A.txt')
b = open('B.txt')
c = open('C.txt', 'w')
for a_line, b_line in zip(a, b):
    c.write(a_line.rstrip() + ' ' + b_line)

a.close()
b.close()
c.close()

      

+3


source


Try this, read files as numpy arrays

 a = np.loadtxt('a.txt')
b = np.genfromtxt('b.txt',dtype='str')

      

In case b, you need genfromtext because of the string content. Than

np.concatenate((a, b), axis=1)

      

Finally, you will receive

np.concatenate((a, b), axis=1)
array([['0.22222', '0.11111', '0.0', 'F', 'F', 'F'],
       ['0.22222', '0.44444', '0.0', 'F', 'F', 'F'],
       ['0.22222', '0.77778', '0.0', 'F', 'F', 'F'],
       ['0.55556', '0.11111', '0.0', 'F', 'F', 'F'],
       ['0.55556', '0.44444', '0.0', 'T', 'T', 'F']], 
      dtype='<U32')

      

+2


source







All Articles