Data Exchange and CSV Handling in Python

data exchange n.w
1 / 13
Embed
Share

Learn about the process of data exchange, the use of CSV and JSON languages, and how to handle CSV files in Python using the csv module. Discover the basics of CSV representation, CSV reader, and CSV writer functions for efficient data manipulation and storage.

  • Data Exchange
  • CSV Handling
  • Python CSV Module
  • Data Formats
  • Programming

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Data Exchange Data Exchange is the process of converting data stored in one format into another format The purpose is to provide a mechanism for sharing data between two programs Data Exchange languages are designed to store general purpose data Two commonly used Data Exchange languages: CSV : Comma Separated Values JSON : JavaScript Object Notation Professor John Carelli, Kutztown University

  2. CSV CSV is used to represent data that can be stored in a table General Properties data is plain text data contains records (on a single line, generally) records are divided into fields separated by a delimiter character (often a comma, hence the name) every record has the same sequence of fields first line is often a header containing names for the field Note: there is no fixed CSV standard, variations exist Professor John Carelli, Kutztown University

  3. Python csv module Python s csv module provides functions that facilitate easy reading and writing of csv formatted files csv.reader creates a csv reader object allows iteration of a file-like object to read contents returns a tuple of strings containing each field csv.writer creates a csv writer object allows iteration of a file-like object for writing writes items in an iterable (ex: list) to the file Professor John Carelli, Kutztown University

  4. csv.reader Basic usage is to create a reader object from file open for reading, then use to it read each line various options available to control the formatting (ex: delimiter, quoting, ) various dialects are supported (like excel ) documentaton # basic csv.reader import csv f = open('stats.csv') reader = csv.reader(f) for line in reader: print(line) Professor John Carelli, Kutztown University

  5. csv.writer Basic usage is to create a reader object from file open for writing, then use it write data to the file various options available to control the formatting (ex: delimiter, quoting, ) various dialects are supported (like excel ) documentaton Professor John Carelli, Kutztown University

  6. csv.writer example # basic csv writer import csv headers = ['Name', 'Level', 'Salary'] with open('output.csv', 'w', newline= ) as f: writer = csv.writer(f) # Output the header line writer.writerow(headers) writer.writerow(['Joe Bob', 'Associate', 55000]) Professor John Carelli, Kutztown University

  7. Basic csv arguments Argument Description One-character string to separate fields (Default is ,) delimiter Varies with OS. Generally, use newline= csv will manage the platform dependency newline Quote characters for fields with special characters (to escape) quotechar quoting What quoting convention to use refer to documentation for more info Professor John Carelli, Kutztown University

  8. JSON JavaScript Object Notation syntax used to create objects in JavaScript a defacto standard for data exchange Features data stored in plain text using keyword:value pairs file extension is .json generally, maps to a Python dict Professor John Carelli, Kutztown University

  9. JSON, basic syntax Data is in name/value pairs of the form "name":value note: name is a string Data separated by commas Curly braces hold objects like a Python dict Square braces hold arrays like a Python list Professor John Carelli, Kutztown University

  10. JSON data types JSON objects can store the following data types String (keys must be strings) Number Object (Another JSON Object) Array (List) Boolean - true/false become Python True/False null (None) Professor John Carelli, Kutztown University

  11. JSON example { "company": "ABC Corp.", "location": "Kutztown", "workdays": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], "employees": [ { "name": "Mary", "email": "mary23@gmail.com"}, { "name": "Bob", "email": "bob32@gmail.com"}, { "name": "Chris", "email": "chris12@gmail.com"} ] } Professor John Carelli, Kutztown University

  12. Python json module Provides functionality for reading or writing JSON files JSON can also be stored in a string Professor John Carelli, Kutztown University

  13. Reading & writing JSON files import json import json with open('results.json`) as f: data = json.load(f) f = open("results.json", "w") # Writes the info stored in # data to the file object 'f' json.dump(data, f) f.close() # print the loaded data print(data) # it will be a dict! print(type(data)) Professor John Carelli, Kutztown University

Related


More Related Content