I have a text file which consists of data including some random data among which there are "names" that exist in separate excel file as rows in a column. What I need to do is to compare strings from txt file and excel and output those that are matching along with some extra data corresponding to that row from different columns. I'd be thankful for some example how to go about it maybe using pandas?
Asked
Active
Viewed 24 times
1 Answers
0
You should open the text and excel file like so:
textdata = open(path_to_file, "r")
exceldata = open(path_to_file, "r")
Then put the data in lists:
textdatalist = [line.split(',') for line in textdata.readlines()]
exceldatalist = [line.split(',') for line in exceldata.readlines()]
And then compare the two lists with:
print(set(exceldatalist).intersection(textdatalist))
All together:
textdata = open(path_to_file, "r")
exceldata = open(path_to_file, "r")
textdatalist = [line.split(',') for line in textdata.readlines()]
exceldatalist = [line.split(',') for line in exceldata.readlines()]
print(set(exceldatalist).intersection(textdatalist))

Martin
- 158
- 1
- 3
- 13
-
After overcoming encoding errors I'm getting TypeError: unhashable type: 'list" with: print(set(exceldatalist).intersection(textdatalist) – Adam Henry Nov 03 '22 at 10:31
-
Check [this post](https://stackoverflow.com/questions/19371358/python-typeerror-unhashable-type-list) I think you have your lists formatted the wrong way. @AdamHenry – Martin Nov 03 '22 at 17:26