def detect_delimiter(self, sample: str) -> str: """Detect CSV delimiter (comma, semicolon, or tab)""" common_delimiters = [',', ';', '\t', '|'] delimiter_counts = {} for delimiter in common_delimiters: if delimiter in sample: # Count occurrences in first line first_line = sample.split('\n')[0] delimiter_counts[delimiter] = first_line.count(delimiter) if delimiter_counts: return max(delimiter_counts, key=delimiter_counts.get) return ',' # Default to comma
def format_date(self, date_str: str) -> str: """Format date for VCF (YYYYMMDD)""" if not date_str: return "" # Try different date formats formats = ['%Y-%m-%d', '%m/%d/%Y', '%d/%m/%Y', '%Y%m%d'] for fmt in formats: try: date_obj = datetime.strptime(date_str, fmt) return date_obj.strftime('%Y%m%d') except ValueError: continue return date_str.replace('-', '').replace('/', '') csv to vcf
def __init__(self): self.version = "3.0" self.encoding_detected = False def detect_encoding(self, file_path: str) -> str: """Detect file encoding automatically""" with open(file_path, 'rb') as f: raw_data = f.read() result = chardet.detect(raw_data) return result['encoding'] or 'utf-8' sample: str) ->
def normalize_column_name(self, col: str) -> str: """Normalize column names to standard format""" col = col.lower().strip() mapping = 'name': 'name', 'fullname': 'name', 'displayname': 'name', 'phone': 'phone', 'mobile': 'phone', 'telephone': 'phone', 'cell': 'phone', 'workphone': 'phone', 'email': 'email', 'emailaddress': 'email', 'company': 'company', 'organization': 'company', 'title': 'title', 'jobtitle': 'title', 'position': 'title', 'address': 'address', 'street': 'address', 'website': 'website', 'url': 'website', 'webpage': 'website', 'birthday': 'birthday', 'bday': 'birthday', 'dob': 'birthday', 'notes': 'notes', 'comment': 'notes', 'description': 'notes' return mapping.get(col, col) str: """Detect CSV delimiter (comma
This complete feature provides a robust, production-ready CSV to VCF converter with extensive error handling, format detection, and customization options.