Json Vcf 変換 _verified_ -
A simple vCard 4.0 example:
NOTE:This is a very long note that spans multiple lines. json vcf 変換
lines.append("END:VCARD") return "\n".join(lines) A simple vCard 4
BEGIN:VCARD VERSION:4.0 FN:John Doe TEL;TYPE=work,voice:+1-555-123-4567 EMAIL:john.doe@example.com END:VCARD suffix parts = value.split('
Converting between JSON and VCF is straightforward for basic contact data but requires careful handling of:
with open(vcf_file_path, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if not line: continue if line == "BEGIN:VCARD": current_contact = {} elif line == "END:VCARD": if current_contact: contacts.append(current_contact) current_contact = None else: prop, value, params = parse_vcf_line(line) if prop == "VERSION": current_contact['version'] = value elif prop == "FN": current_contact['fullName'] = value elif prop == "N": # N:last;first;middle;prefix;suffix parts = value.split(';') current_contact['lastName'] = parts[0] current_contact['firstName'] = parts[1] if len(parts) > 1 else '' current_contact['middleName'] = parts[2] if len(parts) > 2 else '' current_contact['prefix'] = parts[3] if len(parts) > 3 else '' current_contact['suffix'] = parts[4] if len(parts) > 4 else '' elif prop == "EMAIL": if 'email' not in current_contact: current_contact['email'] = [] current_contact['email'].append(value) elif prop == "TEL": if 'phone' not in current_contact: current_contact['phone'] = [] phone_entry = 'number': value if 'type' in params: phone_entry['type'] = params['type'] current_contact['phone'].append(phone_entry) elif prop == "ADR": # ADR: ;street;city;region;code;country parts = value.split(';') # Typically parts[0] = PO box, parts[1] = extended, parts[2] = street, etc. if len(parts) >= 6: current_contact['address'] = 'street': parts[2], 'city': parts[3], 'region': parts[4], 'code': parts[5], 'country': parts[6] if len(parts) > 6 else ''
def parse_vcf_line(line): # Split at first colon if ':' not in line: return None, None, None header, value = line.split(':', 1) # Parse parameters from header parts = header.split(';') prop_name = parts[0] params = {} for p in parts[1:]: if '=' in p: k, v = p.split('=', 1) params[k.lower()] = v else: params['type'] = p # shorthand like TYPE=work return prop_name, value, params