csv, json and python

2
CSV- stands for comma separated values file. This allows data to be saved in a table structured format. CSVs look similar to a spreadsheet but with a .csv extension (they often take the form of a text file containing information separated by commas). CSV files can be used with any spreadsheet program but They differ from other spreadsheet file types because you can only have a single sheet in a file, they cannot save cell, column, or row styling, and cannot save formulas. For example, if you had a table similar to the one below, that data would look like the CSV data below the table. Spreadsheet Table: Data 1 Data 2 Data 3 Example 1 Example 2 Example 3 Example 1 Example 2 Example 3 CSV Table: Each row is a new line, and each column is separated with a comma. Example of CSV in python The CSV module allows you to read and write data in a CSV format. It allows programmers to write data in the format preferred by Excel and read data from a file which was generated by Excel without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special- purpose CSV formats. JSON-stands for JavaScript Object Notation. It is a way to store information in an organized access manner. It gives us a readable collection of data that we can access in a logical manner. JSON Data: JSON has the benefit of having implementations in many languages (especially JavaScript), making it suitable for inter-application communication. JSON is probably most widely used for communicating between the web server and client in an AJAX application, but is not limited to that problem domain. Sarah Dunne import csv # imports the csv module import sys # imports the sys module f = open(sys.argv[1], 'rb') # opens the csv file try: reader = csv.reader(f) # creates the reader object for row in reader: # iterates the { "Red":"#f00", "Green":"#0f0", "Blue":"#00f", Data 1, Data 2, Data 3 Example 1, Example 2, Example 3

Upload: sarah-dunne

Post on 07-Jul-2016

29 views

Category:

Documents


0 download

DESCRIPTION

quick and easy overview of csv and json files

TRANSCRIPT

Page 1: CSV, JSON and Python

CSV- stands for comma separated values file. This allows data to be saved in a table structured format. CSVs look similar to a spreadsheet but with a .csv extension (they often take the form of a text file containing information separated by commas). CSV files can be used with any spreadsheet program but They differ from other spreadsheet file types because you can only have a single sheet in a file, they cannot save cell, column, or row styling, and cannot save formulas.

For example, if you had a table similar to the one below, that data would look like the CSV data below the table.

Spreadsheet Table:

Data 1 Data 2 Data 3Example 1 Example 2 Example 3Example 1 Example 2 Example 3

CSV Table:

Each row is a new line, and each column is separated with a comma.

Example of CSV in python

The CSV module allows you to read and write data in a CSV format. It allows programmers to write data in the format preferred by Excel and read data from a file which was generated by Excel without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats.

JSON-stands for JavaScript Object Notation. It is a way to store information in an organized access manner. It gives us a readable collection of data that we can access in a logical manner.

JSON Data:

JSON has the benefit of having implementations in many languages (especially JavaScript), making it suitable for inter-application communication. JSON is probably most widely used for communicating between the web server and client in an AJAX application, but is not limited to that problem domain.

Take the following string containing JSON data:json_string = '{"first_name": "Guido", "last_name":"Rossum"}'

It can be parsed ( to analyse (a string or text) ) like this:import jsonparsed_json = json.loads(json_string)

And can now be used as a normal dictionary:print(parsed_json['first_name'])"Guido"

Sarah Dunne

Data 1, Data 2, Data 3

Example 1, Example 2, Example 3

Example 1, Example 2, Example

import csv # imports the csv moduleimport sys # imports the sys module

f = open(sys.argv[1], 'rb') # opens the csv filetry: reader = csv.reader(f) # creates the reader object for row in reader: # iterates the rows of the file in orders print row # prints each rowfinally: f.close() # closing

{

"Red":"#f00",

"Green":"#0f0",

"Blue":"#00f",

"Cyan":"#0ff",

"Magenta":"#f0f",

"Yellow":"#ff0",

"Black":"#000"

}