-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinspect.csv.py
More file actions
96 lines (81 loc) · 3.59 KB
/
Copy pathinspect.csv.py
File metadata and controls
96 lines (81 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import csv
def inspect_csv(filepath='./Data/real_data.csv'):
"""Inspect CSV file structure to diagnose parsing issues."""
print("=" * 60)
print("CSV FILE INSPECTION")
print("=" * 60)
# Read first 100 lines to get past comments
with open(filepath, 'r', encoding='utf-8') as f:
all_lines = [f.readline() for _ in range(100)]
# Find where comments end
header_line_idx = None
for i, line in enumerate(all_lines):
if not line.strip().startswith('#') and line.strip():
header_line_idx = i
break
print(f"\nComment lines found: {header_line_idx}")
print("\nLast 5 comment lines:")
print("-" * 60)
if header_line_idx:
for i in range(max(0, header_line_idx - 5), header_line_idx):
print(f"Line {i+1}: {all_lines[i][:100].strip()}")
print("\nFirst data line (header):")
print("-" * 60)
if header_line_idx is not None:
print(f"Line {header_line_idx+1}: {repr(all_lines[header_line_idx][:200])}")
lines = all_lines[header_line_idx:header_line_idx+5]
else:
lines = all_lines[:10]
print("Could not find header - showing first 10 lines")
for i, line in enumerate(lines, 1):
print(f"Line {i}: {repr(line[:200])}")
# Detect delimiter
print("\n" + "=" * 60)
print("DELIMITER DETECTION")
print("=" * 60)
with open(filepath, 'r', encoding='utf-8') as f:
sample = f.read(4096)
sniffer = csv.Sniffer()
try:
dialect = sniffer.sniff(sample)
print(f"Detected delimiter: {repr(dialect.delimiter)}")
print(f"Quote character: {repr(dialect.quotechar)}")
except:
print("Could not auto-detect delimiter")
# Count common delimiters in first data line
first_line = lines[0] if lines else ""
print(f"\nDelimiter counts in first data line:")
print(f" Commas (,): {first_line.count(',')}")
print(f" Tabs (\\t): {first_line.count(chr(9))}")
print(f" Pipes (|): {first_line.count('|')}")
print(f" Semicolons (;): {first_line.count(';')}")
print(f" Spaces: {first_line.count(' ')}")
# Try to read with different delimiters, skipping comment lines
print("\n" + "=" * 60)
print("COLUMN DETECTION ATTEMPTS")
print("=" * 60)
for delim_name, delim in [('comma', ','), ('tab', '\t'), ('pipe', '|'), ('semicolon', ';')]:
try:
with open(filepath, 'r', encoding='utf-8') as f:
# Skip comment lines
for line in f:
if not line.strip().startswith('#'):
# This is the header
header = line.strip().split(delim)
break
print(f"\nWith {delim_name} delimiter ({repr(delim)}):")
print(f" Number of columns: {len(header)}")
if len(header) <= 20:
print(f" Column names: {header}")
else:
print(f" First 10 columns: {header[:10]}")
print(f" Last 5 columns: {header[-5:]}")
# Check if our required columns are present
required = ['koi_disposition', 'koi_score', 'koi_depth', 'koi_model_snr']
found = [col for col in required if col in header]
if found:
print(f" ✓ Found required columns: {found}")
except Exception as e:
print(f"\nWith {delim_name} delimiter: ERROR - {e}")
if __name__ == "__main__":
inspect_csv()