二手产品经理

二手产品经理

THIS IS RENO

Operate csv - 54 days - Learn Python online for 100 days.

Record#

  1. Today, I learned how to manipulate CSV files.
  2. "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()".
  3. The string concatenation function is "str.join('symbol', str)". It joins the elements in the string using the specified symbol.
  4. 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.
  5. When working with CSV files, it is necessary to import the csv module at the beginning of the file.
  6. Today's exercise involves writing a small program to calculate 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.")
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.