1. CSV Format Requirements The converter expects CSV files with the following columns (case-insensitive):
# Check if input file exists if not os.path.exists(args.input): print(f"Error: Input file 'args.input' not found") return 1
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f: f.write(csv_content) f.flush() result = self.converter.convert_to_string(f.name) self.assertIn("FN:John Doe", result) self.assertIn("TEL;TYPE=CELL:+1234567890", result) os.unlink(f.name) csv to vcf
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
This complete feature provides a robust, production-ready CSV to VCF converter with extensive error handling, format detection, and customization options. count contacts converted
def test_basic_conversion(self): csv_content = """Name,Phone,Email John Doe,+1234567890,john@test.com"""
try: count = converter.convert(args.input, args.output, args.encoding) print(f"\n✓ Conversion complete! count contacts converted.") return 0 except Exception as e: print(f"\n✗ Error: str(e)") return 1 if == " main ": exit(main()) 3. Installation Requirements # Install required package pip install chardet Or create requirements.txt echo "chardet>=5.0.0" > requirements.txt pip install -r requirements.txt 4. Usage Examples # Basic conversion python csv_to_vcf.py contacts.csv Specify output file python csv_to_vcf.py contacts.csv -o my_contacts.vcf Specify encoding for non-UTF8 files python csv_to_vcf.py contacts.csv -e iso-8859-1 In Python script from csv_to_vcf import CSVToVCFConverter date_str: str) ->
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('/', '')