[英]Searching a set of keywords in Russian in Python
I am having a problem to import a set of keywords in Russian to a code I'm writing for the the extraction and calculation of those keywords in a corpus of historical texts that I'm working on.我在将一组俄语关键字导入我正在编写的代码中时遇到问题,以便在我正在处理的历史文本语料库中提取和计算这些关键字。 My code looks like this:
我的代码如下所示:
f = open('keyword_rayoni.txt', 'r', 'utf-8')
allKeywords = f.read().lower().split("\n")
f.close()`
print(allKeywords)
I get a TypeError: an integer is required (got type str)我得到一个 TypeError: an integer is required (get type str)
I used the same code on an English set of keywords and it worked.我在一组英文关键字上使用了相同的代码并且它有效。 I also tried to set the encoding for the Russian keywords to UTF-8, but it didn't solve the problem.
我还尝试将俄语关键字的编码设置为 UTF-8,但并没有解决问题。 Could you please help?
能否请你帮忙?
You are using the open
function wrong.您使用的是
open
的 function 错误。 Input help(open)
in a python console.在 python 控制台中输入
help(open)
。 This will give documentation on the open function.这将提供有关开放 function 的文档。 If you read it you will see that the third argument is
buffering
, a different parameter which takes an int
(but you are giving it a string, utf-8
, see?)如果您阅读它,您会看到第三个参数是
buffering
,这是一个不同的参数,它接受一个int
(但您给它一个字符串utf-8
,看到了吗?)
Try: f = open('blah.txt', 'r', encoding='utf-8')
试试:
f = open('blah.txt', 'r', encoding='utf-8')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.