Coding problems

Asked

Viewed 171 times

0

I’m having codage problem that I can’t 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 all right with voc xc3 xaa i like Caf xc3 xa9 this situ xc3 xa7 xc3 xa3o xc3 xa9 Terr xc3 xadvel']

desired output:

hi all right with you

i like coffee

this situation is terrible

I know if I take out the accents (or use python 3) it solves, but unfortunately I can’t do either of those two things.

2 answers

3

You can try using . 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)

1


In this case, as you use output a system terminal, in case you use 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)
  • Didn’t work :(

  • 2

    You are using Linux or Windows?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.