0

I have the following set of simple examples of code, none of which are doing what I want:

import json

a = "u'Kolo Tour\xe9'"
print a

b = (a.decode('cp1252'), 1)
print b

c = (a, 1)
print c

d = ','.join((b.decode('cp1252')))
print d

The final example is throwing up an error about a tuple having no method for decoding. What I want my final item to look like is:

Kolo Touré,1

Can anyone tell me what I am doing wrong and what the correct syntax I need to fix my issue is please?

Thanks

EDIT:

A segment of the actual source data I am attempting to parse in my full scale code prints like this to the screen in both command shell and python IDLE:

(u'Jos\xe9 Enrique', 14230, 29, 3, u'DL', 184, 76, True, False)
4
  • 1
    Why are you starting with a unicode literal represented in a string? Commented Mar 3, 2015 at 17:56
  • @IgnacioVazquez-Abrams Because this is an example of how an item is represented within a tuple in a much larger piece of code I have. The issues begin when I try and decode the unicode item whilst it is part of a tuple. If I decode it before hand, putting it into a tuple seems to undo the decoding and doing it within the tuple throws up an error. Commented Mar 3, 2015 at 18:00
  • That sounds wrong. Please show your source data. Commented Mar 3, 2015 at 18:00
  • @IgnacioVazquez-Abrams I have posted a short segment of the real data I am working on as taking from the screen. I cant post my actual code as there is a lot of it. Commented Mar 3, 2015 at 18:05

1 Answer 1

1

Printing a tuple displays the representation of the contents, so it is extremely unlikely that you have a unicode literal in a string.

newdata = (olddata[0], 1)

EDIT:

Based on your latest output:

u'{},1'.format(olddata[0])

but I'm wondering if you don't want to use csv instead for output generation.

Sign up to request clarification or add additional context in comments.

10 Comments

do you understand my issue though? I need to join the items of the tuple together using a ','.join() statement, but this is causing me an error because the item within the tuple needs decoding.
So... your end result needs to be a string?
yes, and when I attempt the conversion I get an error that the item in question within the tuple could not be decoded,
Yeah... you're going to have to backtrack and figure out what you actually need. Your requirements are making no sense.
I have edited the post to show what I want my final output to look like. please review.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.