首页 > 解决方案 > Colab OSError:[Errno 36] 读取 docx2text 文件时文件名太长

问题描述

我正在研究 NLP 技术,虽然我对 .txt 文件有一些经验,但使用 .docx 很麻烦。我正在尝试在字符串上使用正则表达式,并且由于我使用的是 word 文档,因此这是我的方法:

我将使用 textract 将 docx 转换为 txt 并将字节转换为字符串:

import textract
my_text = textract.process("1337.docx")
my_text = text.decode("utf-8")

我读了文件:

def load_doc(filename):

  # open the file as read only
  file = open(filename, 'r')

  # read all text
  text = file.read()

  # close the file
  file.close()

  return text

然后我尝试做一些正则表达式,例如删除所有数字等,并在主要执行时:

def regextest(doc):

...

...
text = load_doc(my_text)
tokens = regextest(text)
print(tokens)

我得到了例外:

OSError: [Errno 36] File name too long: Are you buying a Tesla?\n\n\n\n - I believe the pricing is...(and more text from te file)

我知道我正在将我的 docx 文件转换为文本文件,然后,当我阅读“文件名”时,它实际上是整个文本。如何保存文件并使其工作?你们将如何处理这个问题?

标签: python-3.xnlpnltkre

解决方案


您似乎正在使用文件的内容-my_text作为filename参数load_doc并因此出现错误。

我认为您宁愿使用实际文件名之一作为参数,可能'1337.docx'而不是该文件的内容。


推荐阅读