问题描述:对A,B文件第一列相同的内容合并成新文件
A文件
1 1 6
2 4 5
3 6 2
4 5 6
B文件
2 4 5
3 6 2
7 6 810 3 5
思路1:直接使用两层循环,一两万数据没问题,几分钟或者更短,笔者90万+数据,跑了一天,卒................................
思路二:采用指针控制游标【第一列应该是有序的】:
举例:比较a,b数组相同的值并输出:
首先两者count1=0,count2=0,然后比较[有一个数组读完即可结束程序],见代码
if a[count1]==b[count2]
count1++
count2++
else if a[count1]<b[count2]
count1++
else
count2++
代码如下:
#include<stdio.h>
int main()
{
int a[7]={1,2,3,4,5,6,7};
int b[8]={5,6,7,8,9,10,27,46};
int index1=0,index2=0;
while(1)
{
if(index1==7 || index2==8)
break;
if(a[index1]==b[index2]){
printf("%d %d\n",a[index1],b[index2]);
index1++;
index2++;
}
else if(a[index1]>b[index2])
index2++;
else{
index1++;
}
}
return 0;
}
好,到此思想应该明白了,本文使用python语言,基于第一行是时间,已经是有序的
python时间格式文本转换
import datetime
a =datetime.datatime.strptime('yourtime1','%Y/%m/%d %H:%M:%S')
b =datetime.datatime.strptime('yourtime2','%Y/%m/%d %H:%M:%S')
print(a==b)
具体Python脚本代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/10/13 20:33
# @Author : Andra
# @Site :
# @File : read.py
# @Software: PyCharm Community Edition
import datetime
fr1 = open("1.txt", 'r')
fr2 = open("2.txt", 'r')
frw = open("3.txt", 'w')
line1 =fr1.readlines()
line2 = fr2.readlines()
count1=0
count2=0
while True:
# check end
if(count1==len(line1) or count2==len(line2)):
break
if (datetime.datetime.strptime(line1[count1].strip("\n").split(",")[0], '%Y/%m/%d %H:%M:%S')) == (datetime.datetime.strptime(line2[count2].strip("\n").split(",")[0], '%Y/%m/%d %H:%M:%S')):
string = line1[count1].strip("\n").split(",")[1] + "," + line2[count2].strip("\n").split(",")[1]
frw.write(string+"\n")
count1 += 1
count2 += 1
elif (datetime.datetime.strptime(line1[count1].strip("\n").split(",")[0], '%Y/%m/%d %H:%M:%S'))<(datetime.datetime.strptime(line2[count2].strip("\n").split(",")[0], '%Y/%m/%d %H:%M:%S')):
count1 += 1
else:
count2 += 1
# close file stream
fr1.close()
fr2.close()
frw.close()
完:
有问题随时交流,共同进步!