Python--UnicodeDecodeError: 'utf

2019-12-10  本文已影响0人  原鸣清

Mac 笔记本上写个脚本把iOS的字符串导成excel文件。遇到如题报错:

Error.png

原因是因为.strings文件的格式问题,执行如下命令解决问题。
dos2unix Localizable.strings

脚本代码如下:

import xlwt
import sys

def set_style(name, height, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style

resource_str=sys.argv[1]

#dos2unix Localizable.strings 解决不同编码的错误问题
lines = open(resource_str ,'r')
strlist = []
for line in lines:
    line = line.strip()
    if line.find('=') > 0 and not line.startswith('#'):
        strs = line.split('=')
        strlist.append([strs[0].strip()[1:-1], strs[1].strip()[1:-2]])
lines.close()

e_file=xlwt.Workbook()
sheet1=e_file.add_sheet(u'sheet1',cell_overwrite_ok=True)
header = [u'Key',u'TW']

for i in range(0,len(header)):
    sheet1.write(0,i,header[i],set_style('Times New Roman',220,True))

count1=1
for origin_item in strlist:
    item1 = '"' + origin_item[0] + '"'
    item2 = '"' + origin_item[1] + '"'
    if len(item1) < 496:
        sheet1.write(count1, 0, item1, set_style('Times New Roman', 220, True))
        sheet1.write(count1, 1, item2, set_style('Times New Roman', 220, True))
        count1 += 1

e_file.save('/Users/leopard/Desktop/testString.xlsx')
上一篇下一篇

猜你喜欢

热点阅读