Python ValueError:基数为10:';的int()的文本无效;。DS#U商店';
我正在实现一个程序,该程序将为xml文件的位置反转索引提供结果。 首先,我需要将文档编号的类型从string更改为int,以便以后使用它 我的一些代码如下所示:Python ValueError:基数为10:';的int()的文本无效;。DS#U商店';,python,shell,terminal,nlp,nltk,Python,Shell,Terminal,Nlp,Nltk,我正在实现一个程序,该程序将为xml文件的位置反转索引提供结果。 首先,我需要将文档编号的类型从string更改为int,以便以后使用它 我的一些代码如下所示: def index(document_directory, dictionary_file, postings_file): # preprocess docID list docID_list = [int(docID_string) for docID_string in os.listdir(docume
def index(document_directory, dictionary_file, postings_file):
# preprocess docID list
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
docID_list.sort()
stemmer = PorterStemmer()
stopwords = nltk.corpus.stopwords.words('english')
# stopwords = set(stopwords.words('english'))
docs_indexed = 0 # counter for the number of docs indexed
dictionary = {} # key: term, value: docIDs containing term (incudes repeats)
# for each document in corpus
for docID in docID_list:
if (LIMIT and docs_indexed == LIMIT): break
.
.
.
.
.
# open files for writing
dict_file = codecs.open(dictionary_file, 'w', encoding='utf-8')
post_file = open(postings_file, 'wb')
.
.
.
.
# close files
dict_file.close()
post_file.close()
.
.
.
.
"""
prints the proper command usage
"""
def print_usage():
print ("usage: " + sys.argv[0] + "-i directory-of-documents -d dictionary-file -p postings-file")
.
.
.
if (RECORD_TIME): start = timeit.default_timer() # start time
index(document_directory, dictionary_file, postings_file) # call the indexer
if (RECORD_TIME): stop = timeit.default_timer() # stop time
if (RECORD_TIME): print ('Indexing time:' + str(stop - start)) # print time taken
现在,当我使用命令运行它时:
$python def_ind.py-i.“/index/”-d“output1111.txt”-p“output222.txt”
我得到以下错误:
Traceback (most recent call last):
File "def_ind.py", line 161, in <module>
index(document_directory, dictionary_file, postings_file) # call the indexer
File "def_ind.py", line 36, in index
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
File "def_ind.py", line 36, in <listcomp>
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
ValueError: invalid literal for int() with base 10: '.DS_Store'
来自我的xml文件的快照:
<DOCNO>1</DOCNO>
<PROFILE>_AN-BENBQAD8FT</PROFILE>
<DATE>910514
</DATE>
<HEADLINE>
FT 14 MAY 91 / (CORRECTED) Jubilee of a jet that did what it was designed
to do
</HEADLINE>
<TEXT>
words, words, words
</TEXT>
<PUB>The Financial Times
</PUB>
<PAGE>
London Page 7 Photograph (Omitted).
</PAGE>
</DOC>`
1
_AN-BENBQAD8英尺
910514
英国《金融时报》1991年5月14日/(更正)一架按设计完成的喷气式飞机的周年纪念
做
言语,言语,言语
英国《金融时报》
伦敦第7页照片(略)。
`
我正在使用python 3.7
注意:我发现许多问题都有相同的错误,但都不适合我的情况。该函数返回特定目录中的文件名
正如错误所述,您正在尝试将这些名称转换为整数。这就是您的错误原因,在这一行:
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]
粘贴的代码乱七八糟(可能是粘贴到StackOverflow后缩进错误);我不明白你想在那里完成什么。就我所见,你从来没有真正使用过列表的值,你只是在上面迭代。那么,为什么还要将值强制转换为
int
?也许我误解了您的问题,但是您正在列出目录中的文件并将名称强制转换为int,并且不知道它为什么会引发异常?我试图从xml文件中获取文档编号。我做错了什么?我是python tho新手。请将您的代码简化为问题的关键部分。好的。我添加了一个xml文件的示例
docID_list = [int(docID_string) for docID_string in os.listdir(document_directory)]