tm

    Есть

список телефонов без "8" fone.csv
953xxxxx45
913xxxxx92
913xxxxx15

и

шаблон phonebook.vcf
BEGIN:VCARD
VERSION:3.0
FN:
N:;;;;
TEL;TYPE=CELL:
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:
N:;;;;
TEL;TYPE=CELL:
END:VCARD
BEGIN:VCARD
VERSION:3.0
FN:
N:;;;;
TEL;TYPE=CELL:
END:VCARD

для вставки в телефонную книгу. Нужно добавить номера телефонов к полю TEL;TYPE=CELL: с добавлением "8"

 

import csv
import os

file_path_csv = os.path.dirname(__file__) + "fone.csv"
file_path_vcf = os.path.dirname(__file__) + "phonebook.vcf"
new_file_path = os.path.dirname(__file__) + "new_phonebook.vcf"

def get_list_phones():
    list_phone = []
    print(file_path_csv)
    with open(file_path_csv, newline='') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
        for row in spamreader:
            phone = str(row).replace("['", "")
            phone = str(phone).replace("']", "")
            list_phone.append(phone)
    return list_phone

def get_vcf_list():
    list_vcf = []
    with open(file_path_vcf, newline='') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
        for row in spamreader:
            line = str(row).replace("['", "")
            line = str(line).replace("']", "")
            list_vcf.append(line)
    return list_vcf

def create_new_vcf():
    list_phones = get_list_phones()
    list_vcf = get_vcf_list()
    if os.path.isfile(new_file_path):
        os.remove(new_file_path)
    file = open(new_file_path, "w+")
    counter = 0
    for line in list_vcf:
        if 'TEL;TYPE=CELL:' in line:
            line = 'TEL;TYPE=CELL:' + '8' + list_phones[counter]
            counter += 1
        file.writelines(line + "\r\n")
    file.close()
    print('Added ', counter + 1, "phones")

if __name__ == "__main__":
    print("start transfer script")
    create_new_vcf()
    print("end transfer script")
Добавить комментарий


Защитный код
Обновить