-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-csv-encoding.ps1
More file actions
87 lines (69 loc) · 3.67 KB
/
Copy pathdebug-csv-encoding.ps1
File metadata and controls
87 lines (69 loc) · 3.67 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
# Debug CSV Encoding Issues
Write-Host "Debugging CSV Encoding Issues" -ForegroundColor Cyan
Write-Host "=============================" -ForegroundColor Cyan
$csvFile = "backend\Datasets\Diabetes.csv"
if (Test-Path $csvFile) {
# Read raw content
$csvContent = Get-Content $csvFile -Raw
Write-Host "`nOriginal CSV Analysis:" -ForegroundColor Yellow
Write-Host "Total length: $($csvContent.Length) characters" -ForegroundColor White
# Count different line ending types
$crlfCount = ([regex]::Matches($csvContent, "`r`n")).Count
$lfCount = ([regex]::Matches($csvContent, "(?<!`r)`n")).Count
$crCount = ([regex]::Matches($csvContent, "`r(?!`n)")).Count
Write-Host "Line endings found:" -ForegroundColor White
Write-Host " - CRLF (\\r\\n): $crlfCount" -ForegroundColor Gray
Write-Host " - LF (\\n): $lfCount" -ForegroundColor Gray
Write-Host " - CR (\\r): $crCount" -ForegroundColor Gray
# Check for other control characters
$controlChars = [regex]::Matches($csvContent, "[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]")
Write-Host " - Other control chars: $($controlChars.Count)" -ForegroundColor Gray
if ($controlChars.Count -gt 0) {
Write-Host "Control characters found at positions:" -ForegroundColor Red
foreach ($match in $controlChars | Select-Object -First 10) {
$char = $match.Value
$ascii = [int][char]$char
Write-Host " Position $($match.Index): ASCII $ascii (0x$($ascii.ToString('X2')))" -ForegroundColor Red
}
}
# Show first few lines in hex
$firstLine = ($csvContent -split "`n")[0]
Write-Host "`nFirst line in hex:" -ForegroundColor Yellow
$bytes = [System.Text.Encoding]::UTF8.GetBytes($firstLine)
$hex = ($bytes | ForEach-Object { $_.ToString("X2") }) -join " "
Write-Host $hex -ForegroundColor Gray
# Test cleaning
Write-Host "`nCleaning test:" -ForegroundColor Yellow
$cleanedCsv = $csvContent -replace "`r`n", "`n" -replace "`r", "`n"
$cleanedCsv = $cleanedCsv -replace "[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]", ""
Write-Host "After cleaning: $($cleanedCsv.Length) characters" -ForegroundColor White
# Test base64 encoding
Write-Host "`nBase64 encoding test:" -ForegroundColor Yellow
# Original (problematic)
$originalBytes = [System.Text.Encoding]::UTF8.GetBytes($csvContent)
$originalBase64 = [System.Convert]::ToBase64String($originalBytes)
Write-Host "Original base64 length: $($originalBase64.Length)" -ForegroundColor Red
# Cleaned
$cleanedBytes = [System.Text.Encoding]::UTF8.GetBytes($cleanedCsv)
$cleanedBase64 = [System.Convert]::ToBase64String($cleanedBytes)
Write-Host "Cleaned base64 length: $($cleanedBase64.Length)" -ForegroundColor Green
# Test JSON encoding
Write-Host "`nJSON encoding test:" -ForegroundColor Yellow
try {
$testObj = @{ csv_data = $originalBase64 }
$testJson = $testObj | ConvertTo-Json -Compress
Write-Host "Original base64 in JSON: SUCCESS (length: $($testJson.Length))" -ForegroundColor Green
} catch {
Write-Host "Original base64 in JSON: FAILED - $($_.Exception.Message)" -ForegroundColor Red
}
try {
$testObj = @{ csv_data = $cleanedBase64 }
$testJson = $testObj | ConvertTo-Json -Compress
Write-Host "Cleaned base64 in JSON: SUCCESS (length: $($testJson.Length))" -ForegroundColor Green
} catch {
Write-Host "Cleaned base64 in JSON: FAILED - $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "CSV file not found: $csvFile" -ForegroundColor Red
}
Write-Host "`nDebugging complete!" -ForegroundColor Cyan