Giter VIP home page Giter VIP logo

csv2json's Introduction

CSV2JSON: A Python Script for Converting CSV Files to JSON

CSV2JSON is a Python script which converts CSV files to their JSON counterparts. Of course, this thing can be done within a couple of lines of Python, but I wanted the output to be in a specific format. Traditionally, if you will convert a list object to a JSON object with indentation, you will see that the resulting string has individual elements occupying whole lines.

[
    "a",
    "b",
    "c"
]

I did not want this. Instead I wanted the list representing a row to be nicely stacked in one single line.

["a", "b", "c"]

I also wanted to implement an automatic delimiter detector for CSV files. The traditional Sniffer() class provided with the csv module was unable to guess the delimiter had I used something like ~ or # as a delimiter. So, I wrote my own delimiter guessing algorithm.

Essentially csv2json.py is a command line utility. Here is how you can use it: NOTE: For the last two modes of operations presented below, CSV files must end with .csv extension else this program won't catch them during directory traversal.

  1. If you wish to convert a single CSV file to a JSON file:
    python csv2json.py path/to/file1/file1.csv -o path/to/file2/file2.json
  1. If you wish to convert all the CSV files present under a directory to their corresponding JSON files in some other directory:
    python csv2json.py -dn path/to/csv/files/ -o path/to/json/files/

Here -d (or --depth) flag denotes the depth till which the first directory should be searched for CSV files.

  1. If you wish to convert all the CSV files present under a directory to their corresponding JSON representations, concatenate these, and write them to a single JSON file, then use -c or --concatenate flag:
    python csv2json.py -dn -c path/to/csv/files/ -o path/to/json/file/file.json

You can play with the data given in samples directory. I am including a Python script play.py which executes a bunch of commands to give you an idea about how to use this utility:

    python play.py

To clean up after running play.py, you can run the same script with clean flag:

    python play.py clean

If you want to know the thought-process behind the code, kindly refer to csv2json.web present in sandbox/. This project was basically developed in sandbox directory as a literate program. When the code was ready, it was moved here.

csv2json's People

Contributors

prankapo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.