You got your hands on some data that was leaked from a social network and you want to help the poor people.
Luckily you know a government service to automatically block a list of credit cards.
The service is a little old school though and you have to upload a CSV file in the exact format. The upload fails if the CSV file contains invalid data.
The CSV files should have two columns, Name and Credit Card. Also, it must be named after the following pattern:
YYYYMMDD
.csv.
The leaked data doesn't have credit card details for every user and you need to pick only the affected users.
The data was published here:
You don't have much time to act.
What tools would you use to get the data, format it correctly and save it in the CSV file?
Do you have a crazy vim configuration that allows you to do all of this inside your editor? Are you a shell power user and write this as a one-liner? How would you solve this in your favorite programming language?
Show your solution in the comments below!
Latest comments (33)
awk:
Just leaving a note here for everyone that would like see more tools and solutions. Checkout the original CSV Challenge.
Using the CSV module to avoid any quoting pitfalls. :)
Ruby is still one of the most pretty languages!
Maybe you can use the
open(url).read
fromrequire 'open-uri'
instead ofcurl
to allow it to run on other systems 🙂Alernatively could look like this:
Oh, I like that!
open-uri
built-in. Also awesome.R
Since the input JSON could be really large, here is a Node.JS steaming version (using stream-json package):
Nice! There is also csv-write-stream then you can save some code :)
Aaaand Rust :)
Really an overkill for this task but fun nevertheless!
A vanilla Node.JS version:
A few things to note:
cache
is a program I wrote that caches command-line invocations, it's to make it cheap to iterate (e.g. so you don't have to hit the network each time) github.com/JoshCheek/dotfiles/blob...My shell is fish (fishshell.com) which allows multi-line editing, and the parentheses in fish are like backticks in bash, so the
> (...)
is redirecting the output into a file whose name is the result of the...
I set myself a time limit of 15 minutes, with no google. I did not know how to download using python, so i used wget or powershell. The rest is straight forward.
PHP:
You beat me to the PHP implementation. And your solution is so elegant.
Or if you guys line nasty oneliners (requre statements don't count)
I'm trying to do it in Elixir now :D
Almost all (except 2 at this time) submission writes csv by hand, not using library. The output will not be valid if a value contains , or "
True. I have not thought of that.
I open the csv in LibreOffice, to make sure it comes out fine, but with really big files, it might not be possible.
Oneliner:
Perl 6? :
Of course in reality you'd probably want to use Text::CSV to properly format the CSV output in order to handle quoting and escaping properly.