Skip to content

Instantly share code, notes, and snippets.

@SahbiOuali13
Last active May 22, 2022 07:44
Show Gist options
  • Save SahbiOuali13/82d06085b9187df0e8fa6c8cf23b2970 to your computer and use it in GitHub Desktop.
Save SahbiOuali13/82d06085b9187df0e8fa6c8cf23b2970 to your computer and use it in GitHub Desktop.
- This can be useful when working with big csv files for example. - You can work with chunks of the data and prevent a potential MemoryError due to the data not fitting into memory. - The impact on our script is processing one line of the entire file
file_name = "techcrunch.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
int(company_dict["raisedAmt"])
for company_dict in company_dicts
if company_dict["round"] == "a"
)
total_series_a = sum(funding)
print(f"Total series A fundraising: ${total_series_a}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment