click below
click below
Normal Size Small Size show me how
Programming #5
From all lessons in "Data Persistance"
Term | Definition |
---|---|
Reading files | with open("poem.txt", "r") as myFile |
Writing to files | myfile = open("poem.txt", "a") |
Overwriting files | myfile.write("Some great text.") |
Reading a line from a file | Reads the line then skips that line if the method is ever called again. file.readline() |
CSV file | Comma-Separated Values. Text file that formats data in a specific way. Each line represents a "record", an object whose fields are printed separated by a comma. Can be used with any separator. Can be read/written by many text editors |
JSON | JavaScript Object Notation is a concise markup language for storing data as key-value pairs. Keys use double quotes. Values can be various types, including arrays and objects |
JSON objects | Inside curly brackets. Contains key-value pairs. Keys in double quotes. Items are separated by commas. Similar to dictionaries |
JSON arrays | Inside square brackets. Contains list of values. Values are separated by commas Similar to lists |
JSON root structure | Must either be an object or an array |
Loading JSON | Import json. Use the load method to turn JSON-formatted string into Python collections and values |
Dumping JSON | the dump() method translates Python data (like dictionaries or lists) into JSON format so it can be stored in a .json file. Ex json.dump(<collection>, file) |
Python objects and JSON | Python objects must be converted to collections like dictionaries for JSON. The __dict__ attribute stores an object's attributes as a dictionary. myDict = person1.__dict__ |