Parse CSV Row Practice Problem
This data science coding problem helps you practice String Manipulation, parse csv row, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of String Manipulation.
- Problem ID: 99
- Problem key: 99-parse-csv-row
- URL: https://datacrack.app/solve/99-parse-csv-row
- Difficulty: medium
- Topic: String Manipulation
- Module: Python Fundamentals
Problem Statement
# š§© Parse CSV Row
---
### šÆ Goal
CSV (Comma-Separated Values) is the most common data format in data science. Parsing a CSV row manually teaches you string splitting, `zip()`, and dictionary building ā and explains exactly what `pd.read_csv()` does under the hood for each row it processes.
---
### š The Task
Given a header string and a data row string (both comma-separated), combine them into a dictionary:
```
header: "name,age,city"
row: "Alice,30,New York"
ā {"name": "Alice", "age": "30", "city": "New York"}
```
---
### š» Task
Implement `parse_csv_row(header, row)`.
---
### š„ Input
- `header`: string ā comma-separated column names
- `row`: string ā comma-separated values
### š¤ Output
- dict mapping each column name to its value (all values as strings)
---
### š§© Starter Code
```python
def parse_csv_row(header, row):
"""
Parse a CSV row into a dictionary using the header as keys.
Args:
header (str): Comma-separated column names
row (str): Comma-separated values
Returns:
dict: {column_name: value} for each column
"""
# š§ TODO: Split both header and row on commas
# š§ TODO: Use zip() to pair column names with values
# š§ TODO: Build a dict from the pairs
pass
```
---
### š” Example
```python
parse_csv_row("name,age,city", "Alice,30,New York")
# Expected: {"name": "Alice", "age": "30", "city": "New York"}
```
---
### š Key Concepts
- `str.split(",")` ā split on a specific delimiter (not just whitespace)
- `zip(keys, values)` ā pairs up two lists element by element
- `dict(zip(...))` ā builds a dict from key-value pairsStarter Code
def parse_csv_row(header, row):
"""
Parse a CSV row into a dictionary using the header as keys.
Args:
header (str): Comma-separated column names
row (str): Comma-separated values
Returns:
dict: {column_name: value} for each column
"""
# š§ TODO: Split both header and row on commas
# š§ TODO: Use zip() to pair column names with values
# š§ TODO: Build a dict from the pairs
pass