minmooongie
minmooongie

Reputation: 7

Expected str instance, int found. How do I change an int to str to make this code work?

I'm trying to write code that analyses a sentence that contains multiple words and no punctuation. I need it to identify individual words in the sentence that is entered and store them in a list. My example sentence is 'ask not what your country can do for you ask what you can do for your country. I then need the original position of the word to be written to a text file. This is my current code with parts taken from other questions I've found but I just can't get it to work

myFile = open("cat2numbers.txt", "wt")   
list = []  # An empty list 
sentence = ""  # Sentence is equal to the sentence that will be entered

print("Writing to the file: ", myFile) # Telling the user what file they will be writing to 
sentence = input("Please enter a sentence without punctuation ") # Asking the user to enter a sentenc
sentence = sentence.lower() # Turns everything entered into lower case
words = sentence.split() # Splitting the sentence into single words
positions = [words.index(word) + 1 for word in words]
for i in range(1,9):
    s = repr(i)
    print("The positions are being written to the file")
    d = ', '.join(positions) 
    myFile.write(positions) # write the places to myFile
    myFile.write("\n")
    myFile.close() # closes myFile
    print("The positions are now in the file") 

The error I've been getting is TypeError: sequence item 0: expected str instance, int found. Could someone please help me, it would be much appreciated

Upvotes: 1

Views: 2540

Answers (2)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160377

The error stems from .join due to the fact you're joining ints on strings.

So the simple fix would be using:

d = ", ".join(map(str, positions))

which maps the str function on all the elements of the positions list and turns them to strings before joining.

That won't solve all your problems, though. You have used a for loop for some reason, in which you .close the file after writing. In consequent iterations you'll get an error for attempting to write to a file that has been closed.

There's other things, list = [] is unnecessary and, using the name list should be avoided; the initialization of sentence is unnecessary too, you don't need to initialize like that. Additionally, if you want to ask for 8 sentences (the for loop), put your loop before doing your work.

All in all, try something like this:

with open("cat2numbers.txt", "wt") as f:   
    print("Writing to the file: ", myFile) # Telling the user what file they will be writing to 
    for i in range(9):
        sentence = input("Please enter a sentence without punctuation ").lower() # Asking the user to enter a sentenc
        words = sentence.split() # Splitting the sentence into single words
        positions = [words.index(word) + 1 for word in words]
        f.write(", ".join(map(str, positions))) # write the places to myFile
        myFile.write("\n")
        print("The positions are now in the file")

this uses the with statement which handles closing the file for you, behind the scenes.

Upvotes: 2

Adrijaned
Adrijaned

Reputation: 450

As I see it, in the for loop, you try to write into file, than close it, and than WRITE TO THE CLOSED FILE again. Couldn't this be the problem?

Upvotes: 0

Related Questions