我的智能手机有问题,所以我需要导出我的地址簿检查和修改每个电话号码。我有一些接触西班牙字符(áñü),当我试图看我的新vcf自制文件雷鸟不承认。 我读过一些相关的问题,但我不知道我的重点在哪里。在
以下是模式:
所以:
在旧vcard中有一些字段:
注:字符集=UTF-8;编码=QUOTED-PRINTABLE:=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使大多数事情正确:)但是我没有在字符串变量中进行任何转换。小心代码中的问题,我读了一些其他帖子,也许重点就在那里)。
我做到了:
我将每一个字符串都设置为这样,以确保它能够显示:
因此,它被添加到vcard中的标记中(现在上面代码中的函数已更改:
^{pr2}$现在在记事本里有这样的字符:
但是
上面的代码只需要字符集
.decode().encode()
修复和IF TOWER
修改即可工作。还要检查文件名以使用您自己的文件名。我在这里发布了对我有用的代码:相关问题 更多 >
编程相关推荐