最近我的诺机破不稳定,说不准哪天就挂了。现在这机器可真不行,当年一个西门子的黑白机我用了4年还好好的,这个诺机用了一年多就要挂了。未雨绸缪啊,先把电话簿备份一下。下了个nokia同步软件,找了一条c53的线,电话本从手机里拷贝出来是一堆VCF文件,每一个联系人一个VCF。都是如下这样的结构(这是一个比较特殊的例子,联系人的名字大于3个汉字,所有在第四行有折行):
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Happy=E7=A6=8F=E9=97=A8=E7=AA=
=97
TEL

REF;VOICE:01088888888
END:VCARD
这样一个一个的看,太不爽了,还是搞个小程序把他们弄到一个excel文档里面看着比较舒服。程序撰写过程中遇到了小问题,QUOTED- PRINTABLE这种格式的处理。我在pythoncn里面发帖很快就有人回复。原来python有专门的库处理这种格式quopri。
程序很小,只有70行。(备份的那些xcf文件在E:\phonebook)
-----------------------
#!/usr/bin/python
# -*- coding: utf8 -*-
""" 诺基亚手机电话本转换excel文件
This is a basic tool to read Nokia phonebook items, and save them to a
standard Exel file or text file.
"""
import os
import re
import urllib
import quopri
from pyExcelerator import *
def GetContactList(directory):
items = os.listdir(directory)
contacts = []
for contact in items:
if contact.endswith('.vcf'):
contacts.append(contact)
return contacts
def GetPhonebook(contact):
"""Return (name, Tel)
"""
info = file(contact).read()
# replace delete \r\n in name new line, transfer quopri type to string
info = quopri.decodestring(info).replace(' ','')
name=phone=''
try:
phone = re.search(r'VOICE:\S+', info).group(0).split(':')[-1]
name = re.search(r'\bN[;:]\S+',info).group(0).split(':')[-1]
name = str(name).decode('utf8')
#name = urllib.unquote(name.replace("=", "%")).decode('utf8')
except:
pass
return name, phone
if __name__ == '__main__':
dir = r'E:\phonebook'
# create xls and working sheet
phonbook = Workbook()
ws = phonbook.add_sheet('nokia')
ws.write( 0, 0, "Name" )
ws.write( 0, 1, "

hone Number" )
i = 1
for item in GetContactList(dir):
name, phone = GetPhonebook( dir + '\\' +str(item) )
if name=='' and phone=='':
continue
ws.write( i, 0, name )
ws.write( i, 1, phone )
i +=1
# save xls
phonbook.save( 'NokiaPhoneBook.xls' )