Spaces:
Running
Running
File size: 1,241 Bytes
77db408 | 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 | import os
import csv
from io import StringIO
def _auto_decode(content: bytes) -> str:
if not content: return ""
# Explicitly try latin-1 if it looks like Visma export (common in Nordics)
if content.startswith(b'\xef\xbb\xbf'):
return content.decode('utf-8-sig')
try:
return content.decode('utf-8')
except:
try:
return content.decode('latin-1')
except:
return content.decode('cp1252', errors='replace')
assets_dir = r"backend/assets"
output = ""
for filename in sorted(os.listdir(assets_dir)):
if filename.endswith(".csv"):
path = os.path.join(assets_dir, filename)
with open(path, 'rb') as f:
content = f.read(4096)
text = _auto_decode(content)
first_line = text.split('\n')[0].strip()
# Clean up potential BOM or weird start
if first_line.startswith('\ufeff'): first_line = first_line[1:]
headers = [h.strip().strip('"') for h in first_line.split(';')]
output += f"File: {filename}\nHeaders: {', '.join(headers)}\n"
output += "-" * 40 + "\n"
with open("backend/headers_report.txt", "w", encoding="utf-8") as f:
f.write(output)
print("Done")
|