Search code examples
pythonsplitreadlines

How to split text in Python 3


My assignment is to open a file and to open a text file and make a list of the all the words used. I am able to open, read, and close the file, however, when I try to split, I get the following error below.

What does this mean and any suggestions?

file = open("decl.txt", "r")
lines = file.readlines()
text.close()

# split oliver
words = re.split('\W+', lines)

print(words)

Error Message

Traceback (most recent call last):
  File "lab.py", line 18, in <module>
    words = re.split('\W+', lines)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/re.py", line 165, in split
TypeError: expected string or buffer

Solution

  • file.readlines() returns a list of all lines, you should use file.read():

    Always use with when handling files, it'll automatically close the file for you.

    with open("decl.txt", "r") as f:
        data = f.read()
    # split oliver
    words = re.split('\W+', data)
    

    Help on file.read:

    >>> print file.read.__doc__
    read([size]) -> read at most size bytes, returned as a string.
    
    If the size argument is negative or omitted, read until EOF is reached.
    Notice that when in non-blocking mode, less data than what was requested
    may be returned, even if no size parameter was given.