0

I am not aware of the cause of this error, but I am trying to take the words within a file, read the lines, split them, and then add those words into a list and sort them. It is simple enough but I seem to be getting an error which states ''str' object cannot be interpreted as an integer' I am not aware of the cause for this error and would appreciate some assistance.

I haven't tried a lot of methods as I was sure this one would work and I don't have a good idea of how to go around it. The file I'm using contains this:

But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

here is the code that I am using...



#userin = input("Enter file name: ")

try:
  l = [] # empty list

  relettter = open('romeo.txt', 'r')
  rd = relettter.readlines() 

  # loops through each line and reads file
  for line in rd:

    #add line to list
    f = line.split(' ', '/n')

    l.append(f)

  k = set(l.sort())

  print(k)

except Exception as e:
    print(e)

the results should print a sorted list of the words present in the poem.

Mia P
  • 73
  • 6

3 Answers3

2

Your giant try/except block prevents you from seeing the source of the error. Removing that:

› python romeo.py 
Traceback (most recent call last):
  File "romeo.py", line 9, in <module>
    f = line.split(' ', '/n')
TypeError: 'str' object cannot be interpreted as an integer

You are passing '/n' as the second argument to the split() method, which is an integer maxsplit. Your line

f = line.split(' ', '/n')

does not work because only one string can be used for the split method, e.g.:

f = line.split(' ')

Note also that '\n' is a newline, not '/n'.

1

You should probably be using with in this case. It esentially manages your otherwise unmanaged resource. Here is a great explanation on it: What is the python keyword "with" used for?.

As for your problem:

with open(fname, "r") as f:
    words = []
    for line in f:
        line = line.replace('\n', ' ')
        for word in line.split(' '):
            words.append(word)

This will read the text line by line and splits each line into words. The words are then added into the list.

If you're looking for a shorter version:

with open(fname, "r") as f:
    words = [word for word in [line.replace('\n', '').split(' ') for line in f]]

This will give a list of words per sentence, but you can flatten and get all your words that way.

Karm
  • 76
  • 4
1

The error is caused when you split f = line.split(' ', '/n') instead do this f = line.split('\n')[0].split(' '). Also on the next statement I think you'lled want to extend not append

try:
    l = [] # empty list

    relettter = open('romeo.txt', 'r')
    rd = relettter.readlines() 

    # loops through each line and reads file
    for line in rd:

        #add line to list
        f = line.split('\n')[0].split(' ')   ##<-first error

        l.extend(f)                          ##<- next problem

    k = set(sorted(l))

    print(k)

except Exception as e:
    print(e)

Though, a much better implementation:

l = [] # empty list

with open('romeo.txt') as file:

    for line in file:
        f = line[:-1].split(' ')
        l.extend(f)

    k = set(sorted(l))
    print(k)
kudeh
  • 883
  • 1
  • 5
  • 16