3

I am very beginner Python. So my question could be quite naive. I just started to study this language principally due to mathematical tools such as Numpy and Matplotlib which seem to be very useful.

In fact I don't see how python works in fields other than maths I wonder if it is possible (and if yes, how?) to use Python for problems such as text files treatment.

And more precisely is it possible to solve such 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 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 which would look like this

   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 containing 3 columns of A.txt and then the 3 columns of B.txt file.

Could someone help me to write the lines in python needed for this?

I could easily do it in fortran but have heard that the script in python would be much smaller. And since I started to study math tools in Python I wish also to extend my knowledge to other opportunities that this language offers.

Thanks in advance

3 Answers 3

4

Of course Python can be used for text processing (possibly it is even better suited for that than for numerical jobs). The task in question, however, can be done 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)
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to concatenate them the good ol' fashioned 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()

10 Comments

rstripping a_line is probably needed
Okay, now I got why from your other comment. Thanks.
I tried this but obtained an eroor message File "<stdin>", line 3 for f in [a, b, c]: ^ SyntaxError: invalid syntax
Line 3? Seems like you're doing something wrong here. I've tried this on my system and it does indeed work.
Well it may depend on system. I will see more precisely later.
|
0

Try this,read files as numpy arrays

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

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

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

Finally, you will get

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

8 Comments

Thanks. I have one more question.
How do I write the obtained array to a file? I tried this c=np.concatenate((a, b), axis=1) np.savetxt('test.txt', c) But I got following error message Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1073, in savetxt fh.write(asbytes(format % tuple(row) + newline)) TypeError: float argument required, not numpy.string_
This concatenate produces an array of strings (U32). savetxt then requires a string compatible fmt, e.g. %s.
It is possible to make an array with mixed float and string (or boolean) fields but that's more complicated, and writing it with savetxt requires a fancier fmt.
Ok, thanks. It works now. I got one more problem. But it's solved now. In fact after concatenation the numbers in the resulting array contained only one digit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.