pandas: Read a file with its separator

Model pd.read_csv(path, sep=SEP): read a table with a given separator and count the missing values pandas would find.

What to build

A program that reports the shape of a table and how many values are missing.

Requirements

  • Print shape (R, C): the number of data rows and of columns.
  • Then print missing M, the number of fields that are empty or exactly one of NA, N/A, NaN, nan, null, NULL, None or n/a.

Input

Line 1: the separator, one character. Line 2: n. Line 3: the header. Then n data rows.

Output

Two lines.

Constraints

1 ≤ n ≤ 1000; every row has as many fields as the header; fields contain no separator.

Example

Input:

;
3
a;b;c
1;2;3
4;NA;6
;null;9

Output:

shape (3, 3)
missing 3

Why it works

The separator decides where one field ends and the next begins, and pandas' default list of missing markers decides which fields become NaN; reading with the wrong separator would give a single column.

Examples

Example 1 · Input
;
3
a;b;c
1;2;3
4;NA;6
;null;9
Expected output
shape (3, 3)
missing 3
Example 2 · Input
,
1
x,y
N/A,none
Expected output
shape (1, 2)
missing 1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Split every line on the given separator.

Hint 2

Compare each field with the set of missing markers.

Solution

Show one correct solution (try it yourself first)
sep = input()
n = int(input())
header = input().split(sep)
rows = [input().split(sep) for _ in range(n)]
markers = {"", "NA", "N/A", "NaN", "nan", "null", "NULL", "None", "n/a"}
print(f"shape ({n}, {len(header)})")
print("missing", sum(1 for r in rows for v in r if v in markers))

Lesson: Reading and writing files →