简体   繁体   English

在Python中连接两个文本文件

[英]Concatenating two text files in Python

I am very beginner Python. 我是非常初学的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. 我刚开始研究这种语言主要是因为Numpy和Matplotlib这样的数学工具似乎非常有用。

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. 事实上,我不知道python如何在除数学之外的字段中工作我想知道是否有可能(如果是,如何?)使用Python来解决诸如文本文件处理之类的问题。

And more precisely is it possible to solve such problem: 更确切地说,可以解决这样的问题:

I have two files A.txt and B.txt . 我有两个文件A.txt和B.txt。 The A.txt file contains three columns of numbers and looks like this A.txt文件包含三列数字,如下所示

 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 : B.txt文件包含三列字母F或T,如下所示:

  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 文件A.txt和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. 换句话说,我需要创建一个包含3列A.txt的文件,然后创建3列B.txt文件。

Could someone help me to write the lines in python needed for this? 有人可以帮我写python所需的行吗?

I could easily do it in fortran but have heard that the script in python would be much smaller. 我很容易在fortran中做到这一点但是听说python中的脚本会小得多。 And since I started to study math tools in Python I wish also to extend my knowledge to other opportunities that this language offers. 自从我开始用Python学习数学工具以来,我也希望将我的知识扩展到这种语言提供的其他机会。

Thanks in advance 提前致谢

Of course Python can be used for text processing (possibly it is even better suited for that than for numerical jobs). 当然,Python可以用于文本处理(可能它甚至比数字作业更适合)。 The task in question, however, can be done with a single Unix command: paste A.txt B.txt > output.txt 但是,有问题的任务可以使用单个Unix命令完成: paste A.txt B.txt > output.txt

And here is a Python solution without using numpy : 这是一个没有使用numpy的Python解决方案:

 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)

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

Try this,read files as numpy arrays 试试这个,将文件作为numpy数组读取

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

In case of b you need genfromtext because of string content.Than 如果是b,你需要genfromtext因为字符串内容.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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM