File: weather_data.csv
Day,Temp (C),Humidity (%)
1,25,65
2,26,70
3,24,62
4,28,75
5,27,68
6,23,60
7,22,58
8,26,70
9,27,72
10,24,64
readfiletolist.py
import csv
weather_data = []
with open('weather_data.csv', 'r') as csvfile:
csvreader = csv.DictReader(csvfile)
for row in csvreader:
weather_data.append({
'Day': int(row['Day']),
'Temp (C)': int(row['Temp (C)']),
'Humidity (%)': int(row['Humidity (%)'])
})
for data in weather_data:
print(data)
Output:
% python3 readfiletolist.py
{'Day': 1, 'Temp (C)': 25, 'Humidity (%)': 65}
{'Day': 2, 'Temp (C)': 26, 'Humidity (%)': 70}
{'Day': 3, 'Temp (C)': 24, 'Humidity (%)': 62}
{'Day': 4, 'Temp (C)': 28, 'Humidity (%)': 75}
{'Day': 5, 'Temp (C)': 27, 'Humidity (%)': 68}
{'Day': 6, 'Temp (C)': 23, 'Humidity (%)': 60}
{'Day': 7, 'Temp (C)': 22, 'Humidity (%)': 58}
{'Day': 8, 'Temp (C)': 26, 'Humidity (%)': 70}
{'Day': 9, 'Temp (C)': 27, 'Humidity (%)': 72}
{'Day': 10, 'Temp (C)': 24, 'Humidity (%)': 64}

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- How to Save and Exit a File in Nano [macOS/Linux/Ubuntu] Terminal - Linux
- How to update Rust on Mac/Linux - Rust
- Go to Specific file path using Mac Finder - MacOS
- Bash command to List Files - Bash
- How to take a screenshot on a Mac - updated for Ventura 13 [updated 2023] - MacOS
- How to Duplicate Terminal in Mac - MacOS
- How to Add New Entry to ZSH PATH - zsh
- Spring Boot: NamedParameterJdbcTemplate batch insert example - Java