- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用 Chardet
以制表符分隔格式推断出非常大的文件(> 400 万行)的编码。
目前,我的脚本可能由于文件大小而出现问题。我想将其缩小到加载文件的前 x 行数,但我在尝试使用 readline()
时遇到了困难。
目前的脚本是:
import chardet
import os
filepath = os.path.join(r"O:\Song Pop\01 Originals\2017\FreshPlanet_SongPop_0517.txt")
rawdata = open(filepath, 'rb').readline()
print(rawdata)
result = chardet.detect(rawdata)
print(result)
它可以工作,但它只读取文件的第一行。我使用简单循环多次调用 readline()
的尝试效果不佳(也许是因为脚本以二进制格式打开文件)。
一行的输出是{'encoding': 'Windows-1252', 'confidence': 0.73, 'language': ''}
我想知道增加它读取的行数是否会提高编码置信度。
如有任何帮助,我们将不胜感激。
最佳答案
我对 Chardet 并不是特别有经验,但是在调试我自己的问题时看到这篇文章,并且很惊讶它没有任何答案。抱歉,如果这对 OP 没有任何帮助为时已晚,但对于其他偶然发现此问题的人:
我不确定读取更多文件是否会改进猜测的编码类型,但您需要做的就是测试它:
import chardet
testStr = b''
count = 0
with open('Huge File!', 'rb') as x:
line = x.readline()
while line and count < 50: #Set based on lines you'd want to check
testStr = testStr + line
count = count + 1
line = x.readline()
print(chardet.detect(testStr))
在我的例子中,我有一个我认为具有多种编码格式的文件,并编写了以下内容以“逐行”测试它。
import chardet
with open('Huge File!', 'rb') as x:
line = x.readline()
curChar = chardet.detect(line)
print(curChar)
while line:
if curChar != chardet.detect(line):
curChar = chardet.detect(line)
print(curChar)
line = x.readline()
关于python - 使用 Chardet 查找超大文件的编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037058/
我刚开始使用 git 和 heroku,我想让我的文件在 heroku 上全天候 24/7 在线,但是在将文件上传到 heroku(git push heroku master) 时出现了这种情况 a
我已经使用 cx_Freeze 创建了一个 exe。 构建成功。 然后当我点击 exe 时,我得到:- no module named 'queue' 所以,我研究并添加了这一行:- from mul
我正在编写一个处理 CSV 文件的程序。这些文件可以有特定的编码。我正在尝试合并一个过程来尝试猜测用户想要使用 chardet 打开的文件的编码。 我正在尝试使用以下代码: rawdata = ope
我正在尝试使用 Chardet 以制表符分隔格式推断出非常大的文件(> 400 万行)的编码。 目前,我的脚本可能由于文件大小而出现问题。我想将其缩小到加载文件的前 x 行数,但我在尝试使用 read
import chardet a='haha' print(chardet.detect(a)) TypeError: Expected object of type bytes or bytearr
您好,我写了一个 python scraper,我在其中导入请求和 Beautiful soup 模块。我正在使用 python2.7 。我正在使用 py2exe 模块从我的 Python 脚本中创建
我正在解码一个大型(大约 1 GB)的平面文件数据库,它随意混合了字符编码。到目前为止,python 模块 chardet 在识别编码方面做得很好,但如果遇到了绊脚石...... In [428]:
在 python3 中导入请求时,出现以下错误: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit
在 python3 中导入请求时,出现以下错误: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit
我在 python 3.2 中使用 chardet 2.01,源代码像这个站点 http://getpython3.com/diveintopython3/case-study-porting-cha
我不知道字符串的编码类型,但我想解码该字符串。我试过 chardet python 模块但没有用。 我知道字符串的输出,无论如何我可以使用 python 解码字符串... 例子 Input '\x06
我正在尝试使用 Python 中的通用编码检测器 (chardet) 来检测文本文件 ('infile') 中最可能的字符编码,并将其用于进一步处理。 虽然 chardet 主要用于检测网页的字符编码
是否有 Java 版本的 python chardet 可以检测 iso-8859-2?我尝试过Mozilla的universalchardet和jchardet,但都不起作用,它们都猜测是windo
我想尝试一些使用chardet 模块 的代码。这是我在网上找到的代码: import urllib2 import chardet def fetch(url): try: result = u
最近我们将 mysql 数据库从 Latin1 移到了 UTF8。在尝试了几种不同的方法来转换它之后,我们无法找到任何不会引入一些非常讨厌的数据丢失的方法(而且许多方法根本什么都不做)。 这让我想知道
最近我们将 mysql 数据库从 Latin1 移到了 UTF8。在尝试了几种不同的方法来转换它之后,我们无法找到任何不会引入一些非常讨厌的数据丢失的方法(而且许多方法根本什么都不做)。 这让我想知道
我似乎找不到这个问题的根本原因。我不知道是 pyinstaller、pip 问题、requests 模块还是其他问题,因为无法最终排除。 我用 python 编写了一个脚本,当我们必须部署一个新单元时
当我尝试调用 BeautifulSoup(page) 时出现以下错误 Traceback (most recent call last): File "error.py", line 10, in
每当我使用请求运行我的代码或进行 pip 安装时,我都会收到此消息 /usr/lib/python2.7/site-packages/requests/__init__.py:91: Requests
我收到的警告似乎未包含在任何其他在线资源中。我安装了 Anaconda Python 3.6。我在创建新的 Conda 环境时收到的警告是: RequestsDependencyWarning: ur
我是一名优秀的程序员,十分优秀!