比较两个文件内容 [英] compare two file contents

查看:85
本文介绍了比较两个文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个文本文件,每个文件包含2个制表符分隔字符串,如下所示:

文件R.txt


class1 12345

class2 26789

class1 4567

Hi
I have two text files and each file contains 2 tab separated strings as below:

File R.txt

class1 12345
class2 26789
class1 4567

推荐答案

您好抱歉,我在完成草稿之前提交了我的帖子。



我有两个文本文件,每个文件包含2个制表符分隔字符串,如下所示:

档案testR.txt

class1 12345

class2 26789

class1 4567

class5 567

class3 987

和另一个文件如下:
文件testP.txt
class5 525

class7 728

class1 670

class8 34

class3 567


我需要比较两个文件,如下所示。对于R.txt中的每个记录,我必须检查P.txt中每个记录中的列1是否相等并进一步处理。我尝试如下。但有些搜索并不完整。对于R.txt中的每个记录,我必须搜索P.txt中的每个记录并进行比较。


我在下面发布了我的脚本。但是逻辑上似乎存在一些缺陷,而且我想知道这种搜索是否是最优的b''cos我的每个文件的记录大小对于R.txt约为5000而对于P.txt约为3000。谢谢,让我知道我的脚本中的问题。

Hi Sorry, I submitted my post before finishing the draft.


I have two text files and each file contains 2 tab separated strings as below:

File testR.txt
class1 12345
class2 26789
class1 4567
class5 567
class3 987

and another file as below:
File testP.txt
class5 525
class7 728
class1 670
class8 34
class3 567

I need to compare both the files as below. For every record in R.txt, I have to check whether the column 1 is equal in the every record in P.txt and process further. I tried as below. But some how the search is not complete. For every record in R.txt, I have to search every record in P.txt and do the comparison.

I have posted my script below. But there seems to be some flaw in the logic and also I want to know whether this kind of search is optimal or not b''cos my record size of each file is around 5000 for R.txt and 3000 for P.txt. Thanks and let me know the problem in my script.

展开 | 选择 | Wrap | 行号


您的代码看起来应该不起作用因为你只是在搜索文件的第一行之后关闭文件P.我不知道该怎么建议,但是因为你想要做的事情对我来说并不清楚。很可能你想使用哈希并搜索哈希而不是一遍又一遍地搜索文件。
Your code looks like it should not work since you are closing file P after only searching the first line of file R. I''m not sure what to suggest though becuase what you are trying to do is not clear to me. Most likely you want to use a hash and search the hashes instead of searching the file over and over.


嗨凯文,


我不能将文件内容添加到哈希中。在上面的例子中,我需要检查file1的第一列和file2的第一列,如果它们相同,我必须进一步处理。基本上我必须检查file1和file2中的每个元素。


谢谢。
Hi Kevin,

I can not add the file contents into a hash. In the above example, I need to check the first column of file1 and first column of file2 and if they are same, I have to process further. Basically I have to check for every element in the file1 and file2.

Thanks.


这篇关于比较两个文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆