-
-
Save Amir-P/d7017ce65da1bd40f4372dfbd2b68ec2 to your computer and use it in GitHub Desktop.
import sys | |
import json | |
input_file = sys.argv[1] | |
output_file = sys.argv[2] | |
output_file = open(output_file, 'w') | |
def to_vcf_string(first_name, last_name, phone): | |
vcf_lines = [] | |
vcf_lines.append('BEGIN:VCARD') | |
vcf_lines.append('VERSION:4.0') | |
vcf_lines.append('N:%s' % (last_name + ';' + first_name)) | |
vcf_lines.append('FN:%s' % (first_name + ('' if len(last_name) == 0 else ' ') + last_name)) | |
vcf_lines.append('TEL:%s' % phone) | |
vcf_lines.append('END:VCARD') | |
vcf_string = '\n'.join(vcf_lines) + '\n' | |
return vcf_string | |
with open(input_file) as json_file: | |
data = json.load(json_file) | |
contacts_list = data['contacts']['list'] | |
for contact in contacts_list: | |
first_name = contact['first_name'] | |
last_name = contact['last_name'] | |
if len(first_name) == 0 and len(last_name) == 0: | |
continue | |
phone = contact['phone_number'] | |
output_file.write(to_vcf_string(first_name, last_name, phone).encode('utf-8')) | |
output_file.close() |
Hi,
Thanks for this helpful code.
I have a suggestion.
replace line 32 with :
output_file.write(vcf_writer(first_name, last_name, phone).encode('utf-8'))
adding the .encode('utf-8')
will convert the arabic and the persian contacts.
Thanks @SHBnik for the suggestion.
I get this error running converter.py:
$ python converter.py Downloads/contacts-2023-02-05.json contacts.vcf
Traceback (most recent call last):
File "/home/thomas/converter.py", line 22, in <module>
contacts_list = data['contacts']['list']
TypeError: list indices must be integers or slices, not str
I get this error running converter.py:
$ python converter.py Downloads/contacts-2023-02-05.json contacts.vcf Traceback (most recent call last): File "/home/thomas/converter.py", line 22, in <module> contacts_list = data['contacts']['list'] TypeError: list indices must be integers or slices, not str
try this:
import sys
import json
def to_vcf_string(first_name, last_name, phone):
vcf_lines = [
'BEGIN:VCARD',
'VERSION:4.0',
f'N:{last_name};{first_name}',
f'FN:{first_name}{" " if last_name else ""}{last_name}',
f'TEL:{phone}',
'END:VCARD'
]
return '\n'.join(vcf_lines) + '\n'
def main(input_file_path, output_file_path):
with open(input_file_path) as json_file:
data = json.load(json_file)
contacts_list = data['contacts']['list']
with open(output_file_path, 'w') as output_file:
for contact in contacts_list:
first_name = contact.get('first_name', '')
last_name = contact.get('last_name', '')
if not first_name and not last_name:
continue
phone = contact.get('phone_number', '')
output_file.write(to_vcf_string(first_name, last_name, phone))
if name == "main":
if len(sys.argv) < 3:
print("Usage: script_name input_file output_file")
sys.exit(1)
main(sys.argv[1], sys.argv[2])
Hello
how give him json file?
Hi. Just run the script passing json file path and desired output path as input like this: python converter.py json_file_path output_file_path
. @MrWesturn
this version of edited code works for me, reads utf-8 characters from JSON file and writes bytes on the output file :
import sys
import json
input_file = sys.argv[1]
output_file = sys.argv[2]
output_file = open(output_file, 'wb')
def to_vcf_string(first_name, last_name, phone):
vcf_lines = [
'BEGIN:VCARD',
'VERSION:4.0',
f'N:{last_name};{first_name}',
f'FN:{first_name}{" " if last_name else ""}{last_name}',
f'TEL:{phone}',
'END:VCARD'
]
vcf_string = '\n'.join(vcf_lines) + '\n'
return vcf_string
with open(input_file, 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
contacts_list = data['contacts']['list']
for contact in contacts_list:
first_name = contact['first_name']
last_name = contact['last_name']
if len(first_name) == 0 and len(last_name) == 0:
continue
phone = contact['phone_number']
output_file.write(to_vcf_string(first_name, last_name, phone).encode('utf-8'))
output_file.close()
Usage:
python converter.py input_file.json output_file.vcf