Programming Principles in Python (CSCI 503)
Programming Principles in Python (CSCI 503)
Modules and Packages
Dr. David Koop
D. Koop, CSCI 503/490, Fall 2022
Reading Files
? Use the open() method to open a le for reading
- f = open('huck-finn.txt')
? Usually, add an 'r' as the second parameter to indicate read (default) ? Can iterate through the le (think of the le as a collection of lines):
- f = open('huck-finn.txt', 'r') for line in f: if 'Huckleberry' in line: print(line.strip())
? Using line.strip() because the read includes the newline, and print writes a newline so we would have double-spaced text
? Closing the le: f.close()
D. Koop, CSCI 503/490, Fall 2022
2
if if
if if
Parsing Files
? Dealing with different formats, determining more meaningful data from les ? txt: text le ? csv: comma-separated values ? json: JavaScript object notation ? Jupyter also has viewers for these formats ? Look to use libraries to help possible
- import json - import csv - import pandas
? Python also has pickle, but not used much anymore
D. Koop, CSCI 503/490, Fall 2022
3
if
if
Writing Files: Use with statement
? outf = open("mydata.txt", "w")
? Methods for writing to a le:
- print(, file= outf) - outf.write() - outf.writelines()
? Make sure to close the le at the end: outf.close() ? With statement does "enter" and "exit": don't need to call outf.close()
- with open('output.txt', 'w') as outf: for k, v in counts.items(): outf.write(k + ': ' + v + '\n')
D. Koop, CSCI 503/490, Fall 2022
4
if if
Assignment 4
? Books in German ? Reading & Writing Files ? Iterators ? Converting certain values ? String Formatting ? CSCI 503 students compute and output statistics to compare authors
D. Koop, CSCI 503/490, Fall 2022
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- programming principles in python csci 503
- lab parse different data types with python networking academy
- python json pointer documentation read the docs
- json quick guide
- accessing r from python using rpy2 byte mining
- dynamic generation of web service apis and interfaces through scu
- search my favorites by color fashion parsing through color classification
- parsing json massachusetts institute of technology
- json or javascript object notation is a lightweight text based open
- xml and json in python
Related searches
- financial management principles in healthcare
- financial management principles in health
- financial principles in healthcare
- finance principles in health care
- adult learning principles in nursing
- ethical principles in the workplace
- 7 ethical principles in nursing
- personal principles in life
- ethical principles in nursing definitions
- 4 ethical principles in medicine
- basic ethical principles in research
- basic ethical principles in business