gpt4 book ai didi

python : number of characters in text file

转载 作者:行者123 更新时间:2023-11-28 22:03:34 25 4
gpt4 key购买 nike

我正在尝试获取文件中的字符数。但是当我在导入的 txt 文件上使用“len”时,它返回的是位数而不是字符数。

text1=open('text1.txt','r+').read()
print len(text1)

1256664

我该如何解决这个问题?

最佳答案

如果问题是您的文件是编码的,比如 UTF-8,那么您应该在计算字符数之前对其进行解码:

utf8_text=open('text1.txt','r+').read()
unicode_data = utf8_text.decode('utf8')

print len(unicode_data)

关于 python : number of characters in text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8931767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com