Csv parse

2 snippets in Python

Also written as parse csv

PYPython

Join & Split

PY · Strings
Syntax
separator.join(iterable)
str.split(separator)
Example
words = ["Python", "is", "great"]
sentence = " ".join(words)
print(sentence)

csv_row = "alice,30,engineer"
fields = csv_row.split(",")
print(fields)
Output
Python is great
['alice', '30', 'engineer']

Note split() with no arguments splits on any whitespace and removes empty strings. split(',') keeps empty strings between consecutive delimiters.

CSV Files

PY · File I/O
Syntax
import csv
Example
import csv

# Writing
with open("users.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=["name", "age"])
    writer.writeheader()
    writer.writerow({"name": "Alice", "age": 30})
    writer.writerow({"name": "Bob", "age": 25})

# Reading
with open("users.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["name"], row["age"])
Output
Alice 30
Bob 25

Note Always pass newline='' when opening CSV files (the csv module handles line endings itself). DictReader/DictWriter are more readable than plain reader/writer.

Frequently asked questions

How does Python handle csv parse?
Python covers this with 2 copy-ready snippets on this page. The "Join & Split" snippet in Python uses `separator.join(iterable)`.
Which code does the Python example use?
The "Join & Split" snippet uses `separator.join(iterable)`, from the Strings section of the Python cheat sheet.
What other Python snippets are shown for "csv parse"?
Besides "Join & Split", this page also shows "CSV Files".
Is there anything to watch out for?
Yes. For "Join & Split": split() with no arguments splits on any whitespace and removes empty strings. split(',') keeps empty strings between consecutive delimiters.