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!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- How to use Content Assist in Eclipse IDE - Eclipse
- [Solution] The connection to the remote PC was lost, preparing to reconnect - Windows RDP - Windows
- How to install Python using Mac Terminal - MacOS
- Create S3 bucket and upload files using AWS Toolkit for Visual Studio Code - AWS
- [Fix] java: integer number too large compilation error - Java
- How to enable root user on Mac Terminal - MacOS
- How to shuffle lines randomly in Sublime Text - Sublime-Text
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command - Bash