# Check if input file exists if not os.path.exists(args.input): print(f"Error: Input file 'args.input' not found") return 1
def test_basic_conversion(self): csv_content = """Name,Phone,Email John Doe,+1234567890,john@test.com""" csv to vcf
def read_csv(self, file_path: str, encoding: Optional[str] = None) -> List[Dict]: """Read CSV file and return list of dictionaries""" if not encoding: encoding = self.detect_encoding(file_path) print(f"Detected encoding: encoding") contacts = [] try: with open(file_path, 'r', encoding=encoding) as f: # Detect delimiter sample = f.read(1024) f.seek(0) delimiter = self.detect_delimiter(sample) print(f"Detected delimiter: delimiter") reader = csv.DictReader(f, delimiter=delimiter) # Normalize column names reader.fieldnames = [self.normalize_column_name(col) for col in reader.fieldnames] for row_num, row in enumerate(reader, start=2): contact = self.normalize_contact(row) if contact.get('name') or contact.get('phone'): contacts.append(contact) else: print(f"Warning: Row row_num skipped - missing name or phone") except Exception as e: raise Exception(f"Error reading CSV file: str(e)") return contacts # Check if input file exists if not os
This complete feature provides a robust, production-ready CSV to VCF converter with extensive error handling, format detection, and customization options. Email John Doe
def escape_vcf_text(self, text: str) -> str: """Escape special characters for VCF format""" if not text: return "" # VCF escaping rules text = text.replace('\\', '\\\\') text = text.replace(';', '\\;') text = text.replace(',', '\\,') text = text.replace('\n', '\\n') text = text.replace('\r', '') return text
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('/', '')
def format_phone(self, phone: str) -> str: """Format phone number for VCF""" if not phone: return "" # Remove spaces and common separators phone = re.sub(r'[\(\)\s\-\.]', '', phone) # Add international prefix if missing if phone.startswith('0') and not phone.startswith('+'): phone = '+1' + phone[1:] # Default to US/Canada, customize as needed return self.escape_vcf_text(phone)