Reputation: 1
I'm trying to get my program to get the sentence from this file so I can use it later on but it gave me an error:
TypeError: expected string or bytes-like object
This is my code:
import os
import re
pass_file = open('writer.odt', 'r')
read = re.findall(r'[ \w]*', pass_file)
print(read)
Upvotes: 0
Views: 640
Reputation: 11
The regex library works on strings. The open file named pass_file in your example is an object that you can get strings from, either one line at a time or a chunk of a set size (in characters or bytes).
To get all uninterrupted sequences of word characters and plain spaces from your file you might replace the second to last line with something like:
read = [re.findall('[ \w]*', line) for line in pass_file]
If you don't want all the zero length strings between each character that is not a word character or a space you can say that the character class must be present one or more times rather than zero or more as your current regex does:
read = [re.findall('[ \w]+', line) for line in pass_file]
These both give you a list containing lists with the occurrences on each line. If you want it all in one list you could substitute something like this:
read = []
for line in pass_file:
read.extend(re.findall('[ \w]+', line))
Upvotes: 1