Record#
- Today, I learned how to manipulate CSV files.
- "with open(file) as f" is a new way to open files. It is more convenient compared to "f=open(file)" because it does not require using "f.close()".
- The string concatenation function is "str.join('symbol', str)". It joins the elements in the string using the specified symbol.
- The CSV function is "csv.DictReader()". It parses the CSV content into a dictionary format, although it is not exactly the same as a real dictionary. The CSV header serves as the key, and the corresponding column is a data item.
- When working with CSV files, it is necessary to import the csv module at the beginning of the file.
- Today's exercise is to write a small program that calculates income. It reads data from a CSV file and performs calculations.
CODE#
import csv
total = 0.0
daytotal = 0.0
with open("Day54Totals.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
total = float(row['Cost']) * float(row['Quantity'])
daytotal += total
print("🌟Shop $$ Tracker🌟")
print(f"Your shop took £{round(daytotal, 2)} pounds today.")
Translation: