Giter Site home page Giter Site logo

csv-mapper's Introduction

Build Status CSV Mapper

Easily manipulate csv files with python. CSV Mapper is a python Module capable of parsing CSV files against a pre-defined mapper file, as well as converting between them.

Installation

CSV-Mapper can be installed using pip or easy_install

$ pip install csvmapper

Another way, which installs the latest updated version is

$ git clone http://github.com/samarjeet27/CSV-Mapper
$ cd CSV-Mapper
$ python setup.py install

Basic Usage

Using Field Mapper

from csvmapper import FieldMapper, CSVParser

fields = ('firstName', 'lastName', 'age')

# if csv file already has a header, use CSVParser('data.csv', hasHeader=True) instead
parser = CSVParser('data.csv', FieldMapper(fields))

data = parser.buildObject() # or parser.buildDict() for a dictionary

A quick snippet to parse files using a dictionary mapper (with type support) -

import csvmapper

# can use csvmapper.JSONMapper, csvmapper.XMLMapper or custom mappers also
mapper = csvmapper.DictMapper([
	[ 
		{ 'name': 'firstName' } , 
		{ 'name' : 'lastName' }, 
		{ 'name': 'age', 'type':'int' }
	]
])

# data.csv
# ------
# John,Doe,39
# James,Bond,29
# Harry,Potter,28

parser = csvmapper.CSVParser('data.csv', mapper)
people = parser.buildObject()
print '%s will be %d years old after 2 years' %(people[0].firstName, (people[0].age + 2))

if your file already as column headers and you don't worry about the type, you can use -

csvmapper.CSVParser('data.csv', hasHeader=True)

Write CSV

# create parser
parser = csvmapper.CSVParser()
dictionary = parser.buildDict() # manipulation works for dict only at the moment
# manipulate csv file
writer = csvmapper.CSVWriter(dictionary) # or CSVObject instance
writer.write('data.csv') # write(filename)

Convert CSV to JSON/XML

import csvmapper
# using same vars as above
converter = csvmapper.JSONConverter(parser) # or csvmapper.XMLConverter
print converter.doConvert(pretty=True)

License

The MIT License

csv-mapper's People

Contributors

jaiswal-ruhil avatar samarsault 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.