Working with files: Difference between revisions
Mr. MacKenty (talk | contribs) No edit summary |
Mr. MacKenty (talk | contribs) (→XML) |
||
Line 22: | Line 22: | ||
=== XML === | === XML === | ||
Please review the [[HTTP, HTTPS, HTML, URL, XML, XSLT, CSS#XML Definition|definition and characteristics of XML]] | Please review the [[HTTP, HTTPS, HTML, URL, XML, XSLT, CSS#XML Definition|definition and characteristics of XML]] XML is a human-readable file format which is also able to be processed by a machine. | ||
==== XML Example ==== | |||
<syntaxhighlight lang=xml> | |||
<CATALOG> | |||
<CD> | |||
<TITLE>Empire Burlesque</TITLE> | |||
<ARTIST>Bob Dylan</ARTIST> | |||
<COUNTRY>USA</COUNTRY> | |||
<COMPANY>Columbia</COMPANY> | |||
<PRICE>10.90</PRICE> | |||
<YEAR>1985</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Hide your heart</TITLE> | |||
<ARTIST>Bonnie Tyler</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>CBS Records</COMPANY> | |||
<PRICE>9.90</PRICE> | |||
<YEAR>1988</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Greatest Hits</TITLE> | |||
<ARTIST>Dolly Parton</ARTIST> | |||
<COUNTRY>USA</COUNTRY> | |||
<COMPANY>RCA</COMPANY> | |||
<PRICE>9.90</PRICE> | |||
<YEAR>1982</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Still got the blues</TITLE> | |||
<ARTIST>Gary Moore</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Virgin records</COMPANY> | |||
<PRICE>10.20</PRICE> | |||
<YEAR>1990</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Eros</TITLE> | |||
<ARTIST>Eros Ramazzotti</ARTIST> | |||
<COUNTRY>EU</COUNTRY> | |||
<COMPANY>BMG</COMPANY> | |||
<PRICE>9.90</PRICE> | |||
<YEAR>1997</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>One night only</TITLE> | |||
<ARTIST>Bee Gees</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Polydor</COMPANY> | |||
<PRICE>10.90</PRICE> | |||
<YEAR>1998</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Sylvias Mother</TITLE> | |||
<ARTIST>Dr.Hook</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>CBS</COMPANY> | |||
<PRICE>8.10</PRICE> | |||
<YEAR>1973</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Maggie May</TITLE> | |||
<ARTIST>Rod Stewart</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Pickwick</COMPANY> | |||
<PRICE>8.50</PRICE> | |||
<YEAR>1990</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Romanza</TITLE> | |||
<ARTIST>Andrea Bocelli</ARTIST> | |||
<COUNTRY>EU</COUNTRY> | |||
<COMPANY>Polydor</COMPANY> | |||
<PRICE>10.80</PRICE> | |||
<YEAR>1996</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>When a man loves a woman</TITLE> | |||
<ARTIST>Percy Sledge</ARTIST> | |||
<COUNTRY>USA</COUNTRY> | |||
<COMPANY>Atlantic</COMPANY> | |||
<PRICE>8.70</PRICE> | |||
<YEAR>1987</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Black angel</TITLE> | |||
<ARTIST>Savage Rose</ARTIST> | |||
<COUNTRY>EU</COUNTRY> | |||
<COMPANY>Mega</COMPANY> | |||
<PRICE>10.90</PRICE> | |||
<YEAR>1995</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>1999 Grammy Nominees</TITLE> | |||
<ARTIST>Many</ARTIST> | |||
<COUNTRY>USA</COUNTRY> | |||
<COMPANY>Grammy</COMPANY> | |||
<PRICE>10.20</PRICE> | |||
<YEAR>1999</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>For the good times</TITLE> | |||
<ARTIST>Kenny Rogers</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Mucik Master</COMPANY> | |||
<PRICE>8.70</PRICE> | |||
<YEAR>1995</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Big Willie style</TITLE> | |||
<ARTIST>Will Smith</ARTIST> | |||
<COUNTRY>USA</COUNTRY> | |||
<COMPANY>Columbia</COMPANY> | |||
<PRICE>9.90</PRICE> | |||
<YEAR>1997</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Tupelo Honey</TITLE> | |||
<ARTIST>Van Morrison</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Polydor</COMPANY> | |||
<PRICE>8.20</PRICE> | |||
<YEAR>1971</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Soulsville</TITLE> | |||
<ARTIST>Jorn Hoel</ARTIST> | |||
<COUNTRY>Norway</COUNTRY> | |||
<COMPANY>WEA</COMPANY> | |||
<PRICE>7.90</PRICE> | |||
<YEAR>1996</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>The very best of</TITLE> | |||
<ARTIST>Cat Stevens</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Island</COMPANY> | |||
<PRICE>8.90</PRICE> | |||
<YEAR>1990</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Stop</TITLE> | |||
<ARTIST>Sam Brown</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>A and M</COMPANY> | |||
<PRICE>8.90</PRICE> | |||
<YEAR>1988</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Bridge of Spies</TITLE> | |||
<ARTIST>T'Pau</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Siren</COMPANY> | |||
<PRICE>7.90</PRICE> | |||
<YEAR>1987</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Private Dancer</TITLE> | |||
<ARTIST>Tina Turner</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>Capitol</COMPANY> | |||
<PRICE>8.90</PRICE> | |||
<YEAR>1983</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Midt om natten</TITLE> | |||
<ARTIST>Kim Larsen</ARTIST> | |||
<COUNTRY>EU</COUNTRY> | |||
<COMPANY>Medley</COMPANY> | |||
<PRICE>7.80</PRICE> | |||
<YEAR>1983</YEAR> | |||
</CD> | |||
<CD> | |||
<TITLE>Pavarotti Gala Concert</TITLE> | |||
<ARTIST>Luciano Pavarotti</ARTIST> | |||
<COUNTRY>UK</COUNTRY> | |||
<COMPANY>DECCA</COMPANY> | |||
<PRICE>9.90</PRICE> | |||
<YEAR>1991</YEAR> | |||
</CD> | |||
</CATALOG> | |||
</syntaxhighlight> | |||
=== JSON === | === JSON === |
Revision as of 13:35, 5 January 2020
![](/images/5/5e/Hierarchical-structure.png)
File input / output[edit]
We use files to store data. For example let us imagine you have a program which functions as a store inventory system. The program allows a user to create, update, read and delete items from a store inventory. If you have no way of saving changes you've made, when you re-start your program, you will need to make all those changes again.
File I/O is the process of reading from a file and writing to a file. These don't happen at the same time (you can't read from a file and write to the same file at the same time).
Some programs only use files to create, read, update and delete data. For example, if we had a program which managed store inventory, every time you made a change (creating a new item, reading an item, updating something about an item, deleting an item) a file would be updated. One advantage of this method is in the event of unexpected power loss, your data would be very safe. Some disadvantages of this method is your program would be much slower and files can be corrupted.
Some programs use internal data structures and then at certain moments save or read that data to a file. For example, if we had a program which managed store inventory, when we started our program the program would read the inventory from a file and put that data into a data structure like a list or dictionary. We could then create, read, update or delete items. When we were ready (for example, exiting the program), we could save those changes into a file. One advantage of this method is the program would be much faster once you had loaded the data in the file into a data structure. One disadvantage might be if you forget to save you would lose all the changes you made since your last save.
Serialized and human-readable[edit]
Serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer)...and reconstructed later (possibly in a different computer environment). When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object.[2]
For example, if you have a complex dictionary data structure with all the items in your store inventory you can serialize that dictionary so it can be perfectly reproduced. You can save the dictionary as a dictionary and then when you unserialize it (read from a file) you will have a dictionary object. One disadvantage of this is serialized files are not human-readable.
A human-readable medium or human-readable format is a representation of data or information that can be naturally read by humans.[3]
File formats[edit]
XML[edit]
Please review the definition and characteristics of XML XML is a human-readable file format which is also able to be processed by a machine.
XML Example[edit]
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary Moore</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Eros</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
<CD>
<TITLE>One night only</TITLE>
<ARTIST>Bee Gees</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Polydor</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1998</YEAR>
</CD>
<CD>
<TITLE>Sylvias Mother</TITLE>
<ARTIST>Dr.Hook</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS</COMPANY>
<PRICE>8.10</PRICE>
<YEAR>1973</YEAR>
</CD>
<CD>
<TITLE>Maggie May</TITLE>
<ARTIST>Rod Stewart</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Pickwick</COMPANY>
<PRICE>8.50</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Romanza</TITLE>
<ARTIST>Andrea Bocelli</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>Polydor</COMPANY>
<PRICE>10.80</PRICE>
<YEAR>1996</YEAR>
</CD>
<CD>
<TITLE>When a man loves a woman</TITLE>
<ARTIST>Percy Sledge</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Atlantic</COMPANY>
<PRICE>8.70</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Black angel</TITLE>
<ARTIST>Savage Rose</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>Mega</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1995</YEAR>
</CD>
<CD>
<TITLE>1999 Grammy Nominees</TITLE>
<ARTIST>Many</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Grammy</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1999</YEAR>
</CD>
<CD>
<TITLE>For the good times</TITLE>
<ARTIST>Kenny Rogers</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Mucik Master</COMPANY>
<PRICE>8.70</PRICE>
<YEAR>1995</YEAR>
</CD>
<CD>
<TITLE>Big Willie style</TITLE>
<ARTIST>Will Smith</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
<CD>
<TITLE>Tupelo Honey</TITLE>
<ARTIST>Van Morrison</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Polydor</COMPANY>
<PRICE>8.20</PRICE>
<YEAR>1971</YEAR>
</CD>
<CD>
<TITLE>Soulsville</TITLE>
<ARTIST>Jorn Hoel</ARTIST>
<COUNTRY>Norway</COUNTRY>
<COMPANY>WEA</COMPANY>
<PRICE>7.90</PRICE>
<YEAR>1996</YEAR>
</CD>
<CD>
<TITLE>The very best of</TITLE>
<ARTIST>Cat Stevens</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Island</COMPANY>
<PRICE>8.90</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Stop</TITLE>
<ARTIST>Sam Brown</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>A and M</COMPANY>
<PRICE>8.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Bridge of Spies</TITLE>
<ARTIST>T'Pau</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Siren</COMPANY>
<PRICE>7.90</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Private Dancer</TITLE>
<ARTIST>Tina Turner</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Capitol</COMPANY>
<PRICE>8.90</PRICE>
<YEAR>1983</YEAR>
</CD>
<CD>
<TITLE>Midt om natten</TITLE>
<ARTIST>Kim Larsen</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>Medley</COMPANY>
<PRICE>7.80</PRICE>
<YEAR>1983</YEAR>
</CD>
<CD>
<TITLE>Pavarotti Gala Concert</TITLE>
<ARTIST>Luciano Pavarotti</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>DECCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1991</YEAR>
</CD>
</CATALOG>
JSON[edit]
Plain Text[edit]
Helpful Links[edit]
- Click here for a cheatsheet for working with Python files
- This is a good newbie guide with examples for working with Python