我的智能手机出现问题,因此我需要导出我的地址簿检查并修改每个电话号码。我有一些西班牙语字符(áñü)的联系方式,当我试图查看我的新vcf自制文件时,Thunderbird无法识别。 我已经阅读了一些相关的问题,但我不知道对我来说有什么意义。
这是架构:
所以:
有一些字段:
请注意;字符集= UTF-8; ENCODING =引用打印:= 6E = 6F1 = 61 = 74 = 61 = 63 = 69 =
这是我的代码打开并解析信息:
def hasRareChar(string):
'''
Checking if strange characters are there.
'''
c = False
i = 0
while True:
if i == len(string): break
if string[i] in 'ñáéíóúÁÉÍÓÚäëïöüÄËÏÖÜ':
c = True
break
i += 1
return c
def codeTag(string):
'''
adds the charset thing
'''
return string[:-1] + ';CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:'
def parseCsvVcard(cab, linea):
'''
sets string info into dict structure,
csv to vcard: I need to re-order and re-name the fields.
'''
# dict splitting info
d = {}
for x, y in zip(cab, linea.split(',')):
# print x + ':' + y
d[x] = y
# ------------------------------------------------
# dict for VCARD format.
d2 = {}
# NAME COMPOSITION - using hasRareChar(str) codeTag(str)
'''
check = ['First Name' in d.keys(),'Last Name' in d.keys(),'Display Name' in d.keys(),_
hasRareChar(d['First Name']),hasRareChar(d['Last Name']),hasRareChar(d['Display Name'])]
tags = ['','','','N:','N:','FN:']
for index, i in enumerate(check[3:]):
if i: tags[index+3] = codetag(tags[index+3])
tags = ['','','',if check[3]: '',,]
'''
# First and Last Names --------
codeNames = hasRareChar(d['First Name'] + d['Last Name'])
strNames = d['Last Name'] + ';' + d['First Name'] + ';;;'
if not codeNames:
d2['N:'] = strNames
else: d2[codeTag('N:')] = strNames
# DISPLAY NAME ----------------
if d['Display Name'] != '' and not hasRareChar(d['Display Name']):
d2['FN:'] = d['Display Name']
elif d['Display Name'] != '':
d2[codeTag('FN:')] = d['Display Name']
else:
if not codeNames:
d2['N:'] = d['First Name'],d['Last Name'] + ";"
else: d2[codeTag('FN:')] = d['First Name'],d['Last Name'] + ";"
# -------IF TOWER:-----------------------------------------
for i in d: # PARA EL RESTO DE CAMPOS NO VACIOS
if i not in ['Display Name', 'First Name', 'Last Name'] and d[i] != '':
if 'Primary Email' == i : #detecto que campo es
tag = 'EMAIL;HOME:'
if 'Secondary Email' == i:
tag = 'EMAIL;WORK:'
if 'Mobile Number' == i:
tag = 'TEL;CELL:'
if 'Home Phone' == i:
tag = 'TEL;HOME:'
if 'Work Phone' == i:
tag = 'TEL;WORK:'
if 'Web Page 1' == i:
tag = 'URL:'
if 'Notes' in i:
tag = 'NOTE:'
if hasRareChar(d[i]): # compruebo si debo codificar el TAG
tag = codeTag(tag)
d2[tag] = d[i].decode() # WHAT SHOULD COME HERE ???????????
else: d2[tag] = d[i] #asigno
return d2
# ----------- MAIN CODE DOWN HERE ---------------------
# -- csv file opened to a string variable ------------
csvFile = open("contactList.CSV",'r')
readed = csvFile.read()
csvFile.close()
lines = readed.split('\n') # split lines
# separated header and info rows.
head = lines[0].split(',')
# la informacion
lines = lines[1:]
# ----------------------------------------
# new text construction with parse function.
texto = ''
for x in lines[:-1]: # last is a blank record
y = parseCsvVcard(head,x)
#print y
texto += 'BEGIN:VCARD\nVERSION:2.1\n'
#iterando cada campo se escribe
for index in y:
texto += str(index)+str(y[index])+'\n'
texto += 'END:VCARD\n'
# ----------------------------------------
# WRITE TO NEW VCARD FILE
with open("please RENAME.vcf", 'w') as vcard:
vcard.write(texto)
print '----- File Created: please RENAME.vcf -----'
print '----- Check it for proper information.'
似乎我维护了charset标记引用,python采用了正确的字符(python使大多数事情正确:)但我没有在字符串变量中进行任何转换。 请注意代码中的问题,我读了一些其他帖子,也许重点是。)
答案 0 :(得分:0)
我管理好了:
我创建了我需要的每个字符串以确保它将显示:
strNames.decode('ISO-8859-1').encode('utf-8')
所以这被添加到vcard中的标签中(现在上面代码中的函数已经改变了:
;CHARSET=UTF-8:
现在记事本中有这种字符:
ñ é
但
- Thunderbird正确显示所有内容。
- 在我的Android手机中它也有效!!
醇>
上面的代码只需要charset .decode().encode()
修复和IF TOWER
修改即可。还要检查文件名以使用您自己的文件名。我在这里发布了适合我的代码:
csvToVCARD.py