Agent Mysterrr
Agent Mysterrr

Reputation: 1

AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

I'm Trying To Create A Small Quiz About Guessing The Name. The Error Comes Up When Im Removing Lowercase Characters From A String, And I Haven't Got A Clue About Fixing It Since I'm Still New To Python. What Would I Need To Do In Order To Solve It?

artists = open('artists.txt') ## IF YOU WANT TO EDIT THE SONG NAMES AND ARTISTS
songs = open('songs.txt') ## YOU NEED TO LEAVE THEM IN ORDER

songfilter = 'abcdefghijklmnopqrstuvwxyz\/' #Lowercase Alphabet With Some Slashes To Remove "/n"

songsFiltered = [songs.replace(alphabet, '') for w in songs]

guessList = list(zip(artists, songs))

songSelect = random.choice(guessList)

print(songSelect)

Im hoping to receive an output, like

('Billie Eilish', 'B G')

Upvotes: 0

Views: 2513

Answers (1)

Kraego
Kraego

Reputation: 3892

song is not an string object (so theres no replace function) in your case it's a file(reader) object (TextIOWrapper).

To get a string you must read() the file,

like:

with open('songs.txt', 'r') as f:
   content = f.read()
   content.replace(alphabet, '')

Same goes for songs in your code, you can't iterate over songs, you have to read the file into a string array/list.

Upvotes: 1

Related Questions