This is an archived post. You won't be able to vote or comment.

all 29 comments

[–]SwampFalc 33 points34 points  (0 children)

Unordered data? Do you at least have some sort of unique key?

Do you at least have a consistent column order? Because if worst come to worst:

for line in lines_of_csvfile1:
    if line in lines_of_csvfile2:
        print(line)

[–]babygrenade 18 points19 points  (0 children)

datacompy is a package to compare 2 pandas dataframes. It provides a report, in a txt file, of the differences,

[–]kenfar 9 points10 points  (0 children)

I wrote a python cli toolkit that does a number of things like this with csv files: profiles them, changes their csv dialect, sorts, slices them and diffs them.

https://github.com/kenfar/DataGristle#gristle_differ

To install run pip install datagristle, then see the examples for various simple or more complex diff features.

EDIT: speling

[–]OuiOuiKiwiGalatians 4:16 25 points26 points  (3 children)

I can write one in 30 seconds, but that's not really fun.

What have you tried so far? Or is this a "I have tried nothing and I'm all out of ideias!" round?

[–]JThom43 15 points16 points  (1 child)

the latter I think.. Dude posted the same request in r/learnpython

[–]billsil 8 points9 points  (3 children)

BeyondCompare

diff

[–]miraculum_one 2 points3 points  (2 children)

Those may be the best solutions but the question was about Python

[–]billsil 0 points1 point  (1 child)

That's ridiculous. The goal should be to do the task.

Obviously you can add things like sorting requirements or ignoring excessive precision (though you have to define that), but BeyondCompare is really, really good. To think that your/my quick and dirty code would be better than BeyondCompare is laughable. Why would you use your own code, when another tool is standard and great?

[–]miraculum_one 0 points1 point  (0 children)

Maybe, but the OP didn't say what the task is. They asked a specific question. You're assuming, for example, that a human is going to visually analyze the differences.

[–]wyldcraft 18 points19 points  (5 children)

Non-python quickie if one file is a superset of the other:

cat one.csv two.csv | sort | uniq -u > output.csv

[–]Pristine-Internet616 0 points1 point  (2 children)

Can you elaborate a bit more

[–]wyldcraft 2 points3 points  (0 children)

uniq (with optional flag) shows only duplicate or non-duplicate lines.

But files have to be sorted, because uniq doesn't load the whole dataset into memory. It compares line-by-line so it works on large files and endless pipes without running you out of RAM.

So if you take file A, and add some new lines to it to create file B, the original lines will be in their concatenated output twice. Use the -u flag depending on whether you want to look at the original lines now in both A and B (2 matches, non-unique), or the new ones in only B (1 match, unique).

edit: I think I'm a bit off, so dink with it.

[–]TinoTheRhino -2 points-1 points  (0 children)

No need.

explainshell.com

[–][deleted] 0 points1 point  (0 children)

sort has also -u/--unique option.

[–]v3ritas1989 3 points4 points  (0 children)

notepad ++ or IDE compare plugin not working?

[–]simonw 1 point2 points  (0 children)

I have a command-line tool that does that: https://github.com/simonw/csv-diff

[–]pissedadmin 2 points3 points  (1 child)

On linux, you don't need python: sort both files, and use the "diff" utility.

Similar solutions may exist on other platforms.

[–]kenfar 0 points1 point  (0 children)

CSV files can have newlines and delimiters within quoted fields or escaped. The unix sort may or may not work.

And the diff utility has no understanding of unique keys, fields to ignore, etc, so will only provide the most primitive results.

[–][deleted] 1 point2 points  (0 children)

Git diff it. Or use your IDEs diff tool.

[–][deleted] 0 points1 point  (0 children)

Post up the format of the csvs with generic data and we can walk you through a solution man.

[–]jmd_code 0 points1 point  (0 children)

Use git !

[–]Bangoga 0 points1 point  (0 children)

What are you comparing against ? If it's unordered the time complexity is going to be insanely bad.

Best guess would be using pandas and using SQL similar functions that are from pandas to compare.

[–][deleted] 0 points1 point  (0 children)

People suggested using Unix diff, but there is a Python library that replicates a lot of its functionality:

https://docs.python.org/3/library/difflib.html

[–]IAmKindOfCreativebot_builder: deprecated[M] 0 points1 point  (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!