python-3.x - 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 文件转换为文本文件,然后,当我阅读“文件名”时,它实际上是整个文本。如何保存文件并使其工作?你们将如何处理这个问题?
解决方案
您似乎正在使用文件的内容-my_text
作为filename
参数load_doc
并因此出现错误。
我认为您宁愿使用实际文件名之一作为参数,可能'1337.docx'
而不是该文件的内容。
推荐阅读
- java - 将给定用户添加到网站用户列表的 Java 方法,前提是没有其他用户具有相同的用户名
- python - PIL TypeError:无法处理此数据类型
- reactjs - 将 authReducer 与 auth token 一起使用是一种好习惯吗?
- java - 如何获取片段着色器的坐标?gl_FragCoord 不工作
- java - 使用 java 对象创建一个简单的数学游戏
- apache-spark - Spark SQL 执行缓慢且资源空闲
- javascript - 如果下拉列表的值包含 unpaid,其他下拉列表将自动只读
- python - 我如何在 PyGame 上正确重新缩放图像而不会被严重裁剪?
- autohotkey - 未知问题,未输入计数器的数字
- c# - C# Web API 在本地机器上工作,但在服务器上不工作