这是我第一次使用 StackOverflow 提问,但多年来你们共同保存了我的许多项目,我已经感到宾至如归了。
我正在使用 Python3.5 和 nltk 来解析完整的古英语语料库,它以 77 个文本文件和一个 XML 文档的形式发布给我,该文档将文件序列指定为 TEI 格式语料库的连续片段。这是 XML 文档 header 的相关部分,表明我们实际上正在使用 TEI:
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader type="ISBD-ER">
<fileDesc>
是的,作为测试,我只是尝试使用 NLTK 的 MTECorpusReader 打开语料库并使用 words() 方法来证明我可以打开它。我正在从交互式 Python shell 中执行所有这些操作,只是为了便于测试。这就是我真正在做的事情:
# import the reader method
import nltk.corpus.reader as reader
# open the sequence of files and the XML doc with the MTECorpusReader
oecorpus = reader.mte.MTECorpusReader('/Users/me/Documents/0163','.*')
# print the first few words in the corpus to the interactive shell
oecorpus.words()
当我尝试这样做时,我得到以下回溯:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/util.py", line 765, in __repr__
for elt in self:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/util.py", line 397, in iterate_from
for tok in piece.iterate_from(max(0, start_tok-offset)):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/util.py", line 291, in iterate_from
tokens = self.read_block(self._stream)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/mte.py", line 25, in read_block
return list(filter(lambda x: x is not None, XMLCorpusView.read_block(self, stream, tagspec, elt_handler)))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/xmldocs.py", line 307, in read_block
xml_fragment = self._read_xml_fragment(stream)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/xmldocs.py", line 252, in _read_xml_fragment
xml_block = stream.read(self._BLOCK_SIZE)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 1097, in read
chars = self._read(size)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 1367, in _read
chars, bytes_decoded = self._incr_decode(bytes)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 1398, in _incr_decode
return self.decode(bytes, 'strict')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 59: invalid start byte
因此,由于我是一名勇敢的 StackOverflowsketeer,我确定一个或多个文件已损坏,或者文件中的某些字符包含 Python 的 utf-8 解码器无法识别的字符处理。我可以相当确定这个文件的完整性(相信我的话),所以我正在追求
我尝试了以下方法来重新格式化 77 个文本文件,但没有明显效果:
for file in loglist:
bufferfile = open(file, encoding='utf-8', errors='replace')
bufferfile.close()
loglist = [name for name in os.listdir('.') if os.path.isfile(name)]
所以我的问题是:
1) 到目前为止我的方法是否有意义,或者到目前为止我在故障排除中是否搞砸了?
2) 基于 UTF-8 错误很早就出现(在十六进制位置 59)和我的 utf- 8 error 替换脚本对问题没有影响?如果我的假设是错误的,那么我怎样才能更好地隔离问题?
3) 如果我们可以断定问题出在 XML 文档上,那么清除它的最佳方法是什么?我尝试找到那个十六进制字节和它对应的ASCII并更改字符是否可行?
预先感谢您的帮助!
最佳答案
您的转换技术没有用,因为您再也没有读取和写回文件。
0x80 不是 UTF-8 或任何 iso-8859-* 字符集中的有效字节。它在 Windows 代码页中有效,但只有 Unicode 可以支持古英文字符,所以你有一些非常破损的数据。
要转换带有坏字节的 UTF-8,请执行以下操作:
with open('input.txt', 'r', encoding='utf-8', errors='ignore') as input,
open('output.txt', 'w', encoding='utf-8') as output:
output.write(input.read())
如果您不关心丢失数据,您可以在 MTECorpusReader 上使用 encoding 参数:
oecorpus = reader.mte.MTECorpusReader('/Users/me/Documents/0163','.*', encoding='cp1252')
这将使 0x80 成为欧元 (€) 符号。
关于utf-8 文件的 Python 3.5 UnicodeDecodeError(语言为 'ang' ,古英语),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38363747/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta