Python CSV Module - Reading Files and Skipping Rows
In this video, we'll learn how to read CSV data from a file into Python applications, using the csv module.
We'll learn about the csv.reader object, and the csv.DictReader object.
We'll also learn how to skip rows in our file that are not relevant to the data, and how to use the next() built-in and itertools.islice() to skip rows.
Finally, we'll quickly demonstrate how easy it is to load CSV data into Pandas DataFrames.
▶️ Full Playlist:
• Python Primers
📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
00:00 Intro
01:22 Reading file with csv reader
03:15 Reading file with csv DictReader
04:58 Skipping row with next() built-in
07:49 Skipping multiple rows with next() built-in
08:16 Skipping rows with itertools islice function
09:45 Reading CSV file with Pandas
☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
To support the channel and encourage new videos, please consider buying me a coffee here:
ko-fi.com/bugbytes
𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
📖 Blog: www.bugbytes.io/posts/
👾 Github: github.com/bugbytes-io
🐦 Twitter: / bugbytesio
📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
NOAA Data: gml.noaa.gov/ccgg/trends/data...
Python CSV module: docs.python.org/3/library/csv...
itertools.islice: docs.python.org/3/library/ite...
Pandas read_csv: pandas.pydata.org/pandas-docs...
#python #csv #pandas
Пікірлер: 20
and here's one that skips ALL commented lines! (i.e. line starts with '#') import csv def read_csv(): ''' Creates a generator with uncmmented rows in csv ''' with open('test.csv', 'r') as csvfile: reader = csv.DictReader(csvfile, delimiter=';') headers = reader.fieldnames for k in reader: if not k[headers[0]].startswith('#'): yield k # --- usage items = read_csv() for item in items: print(item)
@bugbytes3923
Жыл бұрын
Nice!
very useful video on an important topic in Python. The repos and gists are very helpful.
@bugbytes3923
6 ай бұрын
Thanks a lot!
Always got some to learn with you mate. Keep going !
@bugbytes3923
2 жыл бұрын
Thank you mate! Will do!
This really cool!! Your video is very easy to understand!! Hope you will do more video!!
@bugbytes3923
Жыл бұрын
Thank you! Will do!
Great video, helped me solve a DF issue I was having.
@bugbytes3923
Жыл бұрын
Thanks Trevor, I'm glad it helped!
You are a lowkey guy teacher! hope you get big soon, your htmx series was helpful to me!
@bugbytes3923
2 жыл бұрын
Nice! Thank you for that, I'm glad the series helped!
Another great video! Would love to see a tutorial on how to run automated/scheduled tasks from a Django project.
@bugbytes3923
Жыл бұрын
Thanks a lot! There's some stuff on this in the pipeline - just so much to try and get through though 😄
Just saved my uni assignment, thanks lol🤣
@bugbytes3923
Жыл бұрын
Haha nice - good luck with the assignment 😄
Is csvreader the best module for analyzing a csv file? For example, reading from any csv-line, jumping around randomly? For example, I'd like to display a csv-line in a gui (one line at a time), but would like for the user/gui thread to tell the csv thread to skip to line X, or to rewind to 10 lines?
Lovely video. I want to ask a question, if you want to read only one like from your DictReader, analyse the line and remove it from the file
@bugbytes3923
Жыл бұрын
Thank you for watching! This would be easy to do - you can find the row in the CSV that you want to remove, and simply skip over that line while writing all others to another file.
How to skip particularly few rows in between from the csv file