简体   繁体   中英

Python string not recognize special characters

I have a csv with a series of strings with special characters, when it loads looks fine (using df.head(10) but when I get the cell using df.iloc[0].descripcion appears with some encoding.

I see this: depto. interior de 80.15m2, consta de sala comedor cocina equipada, \xc3\xa1rea de lavado, ba\xc3\xb1o completo, recamara principal con closet y ba\xc3\xb1o completo. 2da. recamara con closet.' depto. interior de 80.15m2, consta de sala comedor cocina equipada, \xc3\xa1rea de lavado, ba\xc3\xb1o completo, recamara principal con closet y ba\xc3\xb1o completo. 2da. recamara con closet.'

and I need to change it to something like this: depto. interior de 80.15m2, consta de sala comedor cocina equipada, área de lavado, baño completo, recamara principal con closet y baño completo. 2da. recamara con closet. depto. interior de 80.15m2, consta de sala comedor cocina equipada, área de lavado, baño completo, recamara principal con closet y baño completo. 2da. recamara con closet.

to load the csv I'm using pandas with pd.read_csv('../data.csv')

If I use excel or libre office to open the file looks fine so I think that is the encoding.

This is the code that I'm running at the end

stopwords = set(STOPWORDS)
# split the value 
tokens = df2.iloc[0].descripcion.split(' ')

# Converts each token into lowercase 
for i in range(len(tokens)): 
   tokens[i] = tokens[i].lower() 

comment_words += " ".join(tokens)+" "
wordcloud = WordCloud(width = 1600, height = 1600, 
               background_color ='white', 
               stopwords = stopwords, 
               min_font_size = 10).generate(comment_words) 
 
# plot the WordCloud image                        
plt.figure(figsize = (8, 8), facecolor = None) 
plt.imshow(wordcloud) 
plt.axis("off") 
plt.tight_layout(pad = 0) 
# plt.savefig('words.png',dpi=)
plt.show() ```

Fixed with a combination of df.read_csv(filename,encoding='utf-8')

and adding in the wordcloud instance the path for arial. (you should download and put the right path)

                background_color ='white', 
                stopwords = stopwords,
                font_path='../data/arial.ttf',
                min_font_size = 10).generate(comment_words) ```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM