Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

python 3.x - Colab OSError: [Errno 36] File name too long when reading a docx2text file

I am studying NLP techniques and while I have some experience with .txt files, using .docx has been troublesome. I am trying to use regex on strings, and since I am using a word document, this is my approach:

I will use textract to get a docx to txt and get the bytes to strings:

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

I read the file:

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

I then try and do some regexs such as remove all numbers and etc, and when executing it in the main:

def regextest(doc):

...

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

I get the exception:

OSError: [Errno 36] File name too long: Are you buying a Tesla?



 - I believe the pricing is...(and more text from te file)

I know I am transforming my docx file to a text file and then, when I read the "filename", it is actually the whole text. How can I preserve the file and make it work? How would you guys approach this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems that you are using the contents of the file - my_text as the filename parameter to load_doc and hence the error.

I would think that you rather want to use one of the actual file names as a parameter, possibly '1337.docx' and not the contents of this file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...