Encoding problems

0

I have a coding problem that I can not solve. I've tried several suggested things, but none works:

Script:

# -*- coding: utf-8 -*-

import sys

line = ["Oi     tudo bem com você","eu  GOSTO de café", "esta  situação é  Terrível"]

def suppressions(line):

    #coding = sys.stdout.encoding
    #line_coding = line

    mails = ' '.join(line)
    mails = ' '.join(mails.lower().strip(' ').split()) #tirar os espaços multiplos
    #mails = ' '.join(line.encode(coding))

    return mails

print suppressions(line)

output:

  

['Hi, fine with you, I like caf \ xc3 \ xa9 this situation \ xc3 \ xa7 \ xc3 \ xa3o \ xc3 \ xa9 terr \ xc3 \ xadvel']

desired output:

  Hi, how are you?

     

I like coffee

     

This is a terrible situation.

I know that if I take the accents (or use python 3) it does, but unfortunately I can not do either of these things.

    
asked by anonymous 10.07.2018 / 12:48

2 answers

1

In this case, as you use output a system terminal, in case of using linux, it can be solved as follows

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8') # aqui você seta a codificação

line = ["Oi     tudo bem com você","eu  GOSTO de café", "esta  situação é  Terrível"]

def suppressions(line):
    mails = ' '.join(line)
    mails = ' '.join(mails.lower().strip(' ').split())
    return mails

print suppressions(line)
    
10.07.2018 / 13:18
3

You can try to use .encode ('utf-8')

# -*- coding: utf-8 -*-
import sys

line = ["Oi     tudo bem com você","eu  GOSTO de café", "esta  situação é  Terrível"]

def suppressions(line):
   mails = ' '.join(line.encode('utf-8'))
   mails = ' '.join(mails.lower().strip(' ').split())
   return mails

print suppressions(line)
    
24.07.2018 / 14:18