简体   繁体   English

Python - 合并文件/合并文件

[英]Python - Combine files/Merge files

Hoping you can help.希望能帮到你。 I hold 2 .txt files.我持有 2 个 .txt 文件。 examples below下面的例子

  • .txt1 .txt1

  • 99 1/1/2013 05:10 london (uk/england) gb oval 1800 2/4/2013 99 1/1/2013 05:10 伦敦(英国/英格兰)gb 椭圆形 1800 2/4/2013
  • 70 1/10/1971 21:00 preston (uk/england) gb disk 180 12/19/2003 70 1/10/1971 21:00 普雷斯顿(英国/英格兰)GB 磁盘 180 12/19/2003
  • 6 1/10/1998 18:00 london (uk/england) gb circle 240 9/29/2004 6 1/10/1998 18:00 伦敦(英国/英格兰)gb circle 240 9/29/2004
  • 84 1/10/2002 17:25 nottingham (uk/england) gb circle 3 1/29/2002 84 1/10/2002 17:25 诺丁汉(英国/英格兰)gb circle 3 1/29/2002

.txt2 .txt2

  • 6 "djfdjf" 6"djfdjf"
  • 84 "gfgrtret"第84话
  • 99 "hytyteghej"第99话
  • 70 "yjtytyuytjyj"第70话

output needed需要输出

  • 99 1/1/2013 05:10 london (uk/england) gb oval 1800 2/4/2013 hytyteghej 99 1/1/2013 05:10 伦敦(英国/英格兰)gb 椭圆形 1800 2/4/2013 hytyteghej
  • 70 1/10/1971 21:00 preston (uk/england) gb disk 180 12/19/2003 yjtytyuytjyj 70 1/10/1971 21:00 普雷斯顿(英国/英格兰)GB 磁盘 180 12/19/2003 yjtytyuytjyj
  • 6 1/10/1998 18:00 london (uk/england) gb circle 240 9/29/2004 djfdjf 6 1/10/1998 18:00 伦敦(英国/英格兰)gb circle 240 9/29/2004 djfdjf
  • 84 1/10/2002 17:25 nottingham (uk/england) gb circle 3 1/29/2002 gfgrtret 84 1/10/2002 17:25 诺丁汉(英国/英格兰)gb 圈 3 1/29/2002 gfgrtret

As you can see, they are not sorted in numerical order.如您所见,它们不是按数字顺序排序的。 i have been advised however that they can be 'defined' and .txt2 can be added to the end of each line in .txt2.然而,我被告知它们可以被“定义”并且 .txt2 可以添加到 .txt2 中每行的末尾。

Can anyone assist with the code i would require to do this?任何人都可以协助我执行此操作所需的代码吗?

ignore the bullet points忽略要点

i have code that works already for what i require, however this seems out of my zone currently.我的代码已经可以满足我的需要,但是目前这似乎超出了我的范围。

Is this good enough for you?这对你来说足够了吗? I used pandas for simplicity, but you could avoid it.为简单起见,我使用了熊猫,但您可以避免使用它。 I changed the order of the rows, but there must be an easy way to avoid it in case that's a problem.我更改了行的顺序,但必须有一种简单的方法来避免它,以防出现问题。

import pandas as pd

data1 = pd.read_csv('text1.txt', sep=" ", index_col=0, header=None)
data2 = pd.read_csv('text2.txt', sep=" ", index_col=0, header=None)

result = pd.concat([data1, data2], axis=1, sort=True)

result.to_csv(r'out.txt', header=None,  sep=' ', mode='a')

They are sorted by index它们按索引排序

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

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