区别于两个文件 [英] Take difference from two files
本文介绍了区别于两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi Team,
我是python的初学者,所以我需要你的帮助来解决这个问题。
我喜欢从file1获取字符串并在file2中搜索并存储数组中的数据。
Ex-abhishek= 0 file1
abhishek= 1 file2
然后我必须在数组中存储值0和1 。
我尝试了什么:
我是python的新手所以我没有太多的想法
Hi Team,
I am beginner in python so i required your help for solving the issue.
I like to take strings from file1 and search in file2 and stored the data in array .
Ex- "abhishek" =0 file1
"abhishek" =1 file2
then i have to store both values 0 and 1 in array .
What I have tried:
I am new to python so i do not have so much idea
推荐答案
有点晚了但这里有一些有用的信息。
这些文件中的数据格式是什么?如果您正在进行文件读取操作,则需要知道这些文件中的数据不是随机放置的。因此,例如,如果文件包含字符串和由逗号分隔的行中的值(基本上是.csv文件),则每行应采用此格式。
在上面这个例子中,代码看起来像这样:
A bit late but here is some useful information.
What format is the data in these files? If you are doing a file read operation, you need to know that the data in these files are not just randomly placed. So, for example, if the file contains a string and a value in a line separated by a comma (.csv file basically), every line should be in this format.
In this above example, the code would look something like this:
f = open("C:\\File1.txt")
file1 = {} #empty dictionary that will be filled with the file data
for line in f:
temp = list(line.split(",")) #dealing line by line, in each line we split using the comma as delimiter
file1[temp[0]]=temp[1]
#Do same for File 2
现在您可以在词典中找到数据然后搜索并操纵
您可以阅读此链接和这个。
这篇关于区别于两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文