4 Replies - 859 Views - Last Post: 30 June 2011 - 08:08 PM Rate Topic: -----

#1 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

String error

Posted 30 June 2011 - 11:52 AM

String test contains any type of character and i only want a-z. However when i remove redundant characters, i want to leave that space empty which is why i replace with a space (" "). However my find method returns error regarding a unicode char. I can only presume its the space, but i need the space or the rest of my prog doesnt work.

index = text.find(search)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)



new_string = re.sub("[^a-zA-Z]"," ", test) 

 index = text.find(search)


Im stuck. Is the sapce a unicode char?

Is This A Good Question/Topic? 0
  • +

Replies To: String error

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: String error

Posted 30 June 2011 - 11:58 AM

unless this is a massively big application, I'd still recommend the approach I showed you before.

here's a little interactive output:

>>> x = "KLSDN)(JU$R-0si-jsldkfnmlkj"
>>> y = ""
>>> for letter in x:
	if letter.isalpha():
		y += letter
	else:
		y+= " "

		
>>> y
'KLSDN  JU R  si jsldkfnmlkj'

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: String error

Posted 30 June 2011 - 12:03 PM

No the space is not a unicode character (or more accurately it belongs to the ASCII subset of Unicode). Apparently your text string contains a unicode character (or at least a non-ASCII character) at index 0.
Was This Post Helpful? 0
  • +
  • -

#4 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

Re: String error

Posted 30 June 2011 - 12:04 PM

atraub - your method is perfect but I still get my ascii code error when applying the string to the find method. It doesnt like the spaces im putting in the string and i dont understand why?

View Postsepp2k, on 30 June 2011 - 01:03 PM, said:

No the space is not a unicode character (or more accurately it belongs to the ASCII subset of Unicode). Apparently your text string contains a unicode character (or at least a non-ASCII character) at index 0.



Sepp - how to i get rid of it? as far as im aware it doesnt, is there a way to check or just remove it?



Or even just remove anything that is unicode?

This post has been edited by tootypegs: 30 June 2011 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: String error

Posted 30 June 2011 - 08:08 PM

Post the input that is causing the error. If it's a file, then upload it and attach it to your post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1