(1条消息) python 文件操作truncate报错_alienyo99的博客-CSDN博客https://blog.csdn.net/m0_68190354/article/details/123679828?spm=1001.2014.3001.5502之前的博文里提到了truncate要对应字节数与字符数,但是不可能人工去计数,因此,试写了一个计数函数,可能对此问题有些许帮助。
def en_cn(str_,position):
if type(position) == str:
index = str_.find(position)
if type(position) == int:
index = position
content = str_[0:index]
cn = []
en = []
for word in content:
if len(word.encode('utf-8')) == 3:
cn.append(word)
if len(word.encode('utf-8')) == 1:
en.append(word)
byte = 3*len(cn)+len(en)
return byte