0

I have some unicode data in the following format:

 [(u"'goal','corner','rightfoot'", u'1'), (u"'goal','directfreekick','leftfoot'", u'1')
, (u"'goal','openplay','leftfoot'", u'2'), (u"'goal','openplay','rightfoot'", u'2'),
 (u"'miss','corner','header'", u'5'), (u"'miss','corner','rightfoot'", u'1'), 
(u"'miss','directfreekick','leftfoot'", u'1'), (u"'miss','directfreekick','rightfoot'", u'2'),
(u"'miss','openplay','header'", u'4'), (u"'miss','openplay','leftfoot'", u'14'), 
(u"'miss','openplay','rightfoot'", u'16')]

This is contained within a variable that for now I will call var. I want to convert this to a regular string using str(var), however this does not seem to be having any effect.

Can anyone see what I am doing wrong here?

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • what do you call a regular string? are you looking to encode the unicode data into a particular format (UTF-8, ASCII,...)? – isedev Sep 16 '14 at 20:10
  • I don't think `str(var)` will do what you want, because it's effectively "turn this list into a string". You want "turn each element of each tuple in this list into a string", which is something else entirely. In any case, if `str` is literally having no effect at all, I suspect you're doing `str(var)` without assigning the result to anything. At least `var = str(var)` should have an observable effect. – Kevin Sep 16 '14 at 20:13
  • It *is* a regular string. `str` is the weird type. – Ignacio Vazquez-Abrams Sep 16 '14 at 20:19
  • @kevin i have assigned the str conversion to another variable. – gdogg371 Sep 16 '14 at 20:21

1 Answers1

2

This might do what you want:

var = [tuple(s.encode('ascii', 'replace') for s in t) for t in var]

Basically this loops over every tuple in var, takes every unicode string from that tuple and calls encode() on that unicode string.

But you should really read this: Convert a Unicode string to a string in Python (containing extra symbols)

Community
  • 1
  • 1
rje
  • 6,388
  • 1
  • 21
  • 40
  • hi, thanks for replying. that didnt work unfortunately. – gdogg371 Sep 16 '14 at 20:18
  • Why not? What did/didn't it do? – rje Sep 16 '14 at 20:25
  • My statement returns a new list (var doesn't change), I guess you need to assign it to var if you want to update that variable. Will change the answer. – rje Sep 16 '14 at 20:28
  • sorry, that was a really dumb mistake on my part. i forgot to assign your expression to a variable. that works now, thanks a lot. – gdogg371 Sep 16 '14 at 20:30