4Geeks logo
4Geeks logo
About us

Learning library

For all the self-taught geeks out there, here our content library with most of the learning materials we have produces throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer

Data Science and Machine Learning - 16 wks

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

← Back to Lessons

Continue learning for free about:

Edit on Github

Working with Plain Text Files

Files are the only way to store on a computer.
Different file formats

Files are the only way to store on a computer.

Your entire computer hard drive is comprised of files, is the most low-level way to store information, your computer runs on a file system or file directory control that how data is stored and retrieved.

Using a backend language you can access the majority of the computer files, and that gives you almost endless power!

Retrieving data from files

Let's say that you have the bitcoin prices from the last day in a bitcoin_prices.csv file with the following format:


📺 Five minute video explaining what is a CSV file

Basically, every line in the CSV file represents one price, for example:

1Currency,Date,Closing Price (USD),24h Open (USD),24h High (USD),24h Low (USD) 2BTC,2019-10-29,9455.7246926058,9228.0745024715,9551.7787262272,9125.7784571584

You can read the file with any backend programing language and interpret it based on the positions of the values:

1import csv, json 2 3file = open("bitcoin_prices.csv", "r") 4csv_f = csv.reader(file) 5 6prices = [] 7for row in csv_f: 8 prices.append({ 9 "ticker": row[0], 10 "price": row[2] 11 })

Saving data into files

Let's say you are running a script that has a variable called todos that contains a todo list:

1todos = ['make the bed', 'do the laundry', 'finish homework']

That variable is being stored in the RAM memory until you decide to save it to a text file or database. The RAM memory is not reliable because your computer could lose power at any moment (turned off).

You can save that variable into a todos.csv file with the following python code:

1todos = ['make the bed', 'do the laundry', 'finish homework'] 2 3todos_as_csv = ','.join(todos) # convert the list into a string 4file = open('todos.csv', 'w+') # open the file for writing 'w', create if it doesn't exist 5file.write(todos_as_csv) # write the content 6file.close() # close the file

The code above will create or update a todos.csv with content similar to this:

1make the bed, do the laundry, finish homework

Different file formats

CSVComma , separated values, one line for each row or different entity.
JSONVery similar to Javascript syntax, made especially for developers and the most used format when transmitting information over the internet (HTTP)
Yaml or YMLThe easiest format to understand, developers love it because it is fast but it's also very similar to a simple text file, it allows comments and uses indentation instead of commas or braces for delimitation
XMLVery popular in the 90's and still being used in a lot of legacy software

Converting from CSV Text to Python Object in memory

1import csv 2file = open("bitcoin_prices.csv", "r") 3file_content = csv.reader(file) 4for row in file_content: 5 print("First element: " + row[0]) 6 print("Second element: " + row[1]) 7 # etc..

Converting from JSON Text to Python Object in memory

1import json 2filePointer = open("bitcoin_prices.json", "r") 3data = json.load(filePointer) 4prices = [] 5for row in data: 6 print("First element: " + row["ticker"]) 7 print("Second element: " + row["date"]) 8 # etc..

Converting from Yaml Text to Python Object in memory

1import yml #you have to install pip package pyyaml 2 3filePointer = open("bitcoin_prices.yml", "r") 4data = yaml.load(filePointer) 5prices = [] 6for row in data: 7 print("First element: " + row["ticker"]) 8 print("Second element: " + row["date"]) 9 # etc..

Here is a live demonstration loading all three types of files.