Python CSV Demo

By Dr. Louay Chebib




https://github.com/Netuser-Py/pythoncsvdemo

Python CSV Demo

  • What is CSV
  • Python CSV
  • Demo

Python CSV Demo

What is CSV?

  • CSV = Comma Separated Values file
  • Plain Text file with record delimiters (\r\n)
  • Usually Comma or Tab delimited fields with or without quotes (") for text fields
  • Generally used with Spreadsheets Such as MS Excel
  • Database Structure is First Normal Form

Python CSV Demo

Python CSV

  • Python Native CSV support with Import csv
  • csv.list_dialects() -> ['excel', 'excel-tab', 'unix']

  • The reader is hard-coded to recognise either '\r' or '\n' as end-of-line,  and ignores lineterminator.

  • lineterminator="\n" (unix); lineterminator="\r\n" is ignored

  • for tab, use: delimiter='\t' in the open

  • csv.Sniffer is used to deduce the format of a CSV file

  • Related Excel functions in openpyxl

Python CSV Demo

Demo

  • Start with an Excel file (CLNTCodesDomo1.xlsx) and save as comma delimited CSV
  • Note the multi-line cell
  • load_CLNT():

    • pre-process: replace single b'\n' -> b'|' 

    • uses tempfile.TemporaryFile()
  • write_test(): write tempfile to disk
  • input_CSVfile(): load to Hold_recs
  • Make_sheet(): write to a workbook

Python CSV Demo

By Dr. L. Chebib

Python CSV Demo

Python CSV Demo

  • 1,364