1

I'm taking user inputted strings continuously and then trying to remove any character that is not a character or digit.

The method that I developed involves splitting a string by the white space and then analyzing each word to find invalid characters.

I'm having a hard time putting the words back together with spaces in between each word. I've tried using ' '.join(list) but it puts a space in between each character or digit.

4

4 回答 4

4

当然,@Ashwini 的答案比这更好,但如果你仍然想用循环来做

strings = raw_input("type something")
while(True):
    MyString = ""
    if strings == "stop": break
    for string in strings.split():
        for char in string:
            if(char.isalnum()): MyString += char
        MyString += " "
    print MyString
    strings = raw_input("continue : ")

样品运行

$ python Test.py
type somethingWelcome to$%^ Python
Welcome to Python 
continue : I love numbers 1234 but not !@#$
I love numbers 1234 but not  
continue : stop

编辑

Python 3 版本:

正如 Ashwini 在评论中指出的那样,将字符存储在列表中并在末尾打印带有 join 的列表。

strings = input("type something : ")
while(True):
    MyString = []
    if strings == "stop": break
    for string in strings.split():
        for char in string:
            if(char.isalnum()): MyString.append(char)
        MyString.append(" ")
    print (''.join(MyString))
    strings = input("continue : ")

样品运行:

$ python3 Test.py
type something : abcd
abcd 
continue : I love Python 123
I love Python 123 
continue : I hate !@#
I hate  
continue : stop
于 2013-07-06T20:03:20.990 回答
2

基于简单循环的解决方案:

strs = "foo12 #$dsfs 8d"
ans = []
for c in strs:
    if c.isalnum():
        ans.append(c)
    elif c.isspace():  #handles all types of white-space characters \n \t etc.
        ans.append(c)
print ("".join(ans))
#foo12 dsfs 8d

单线:

使用str.translate

>>> from string import punctuation, whitespace
>>> "foo12 #$dsfs 8d".translate(None,punctuation)
'foo12 dsfs 8d'

要同时删除空格:

>>> "foo12 #$dsfs 8d".translate(None,punctuation+whitespace)
'foo12dsfs8d'

regex

>>> import re
>>> strs = "foo12 #$dsfs 8d"
>>> re.sub(r'[^0-9a-zA-Z]','',strs)
'foo12dsfs8d'

使用str.join,str.isalnumstr.isspace:

>>> strs = "foo12 #$dsfs 8d"
>>> "".join([c for c in strs if c.isalnum() or c.isspace()])
'foo12 dsfs 8d'
于 2013-07-06T19:53:11.017 回答
0

这是我的解决方案。有关更多信息,请参阅评论:

def sanitize(word):
    """use this to clean words"""
    return ''.join([x for x in word if x.isalpha()] )

n = input("type something")

#simpler way of detecting stop
while(n[-4:] != 'stop'):
    n += "  " + input("continue : ")

n = n.split()[:-1]
# if yuo use list= you are redefining the standard list object
my_list = [sanitize(word) for word in n]

print(my_list)
strn = ' '.join(my_list)
print(strn)
于 2013-07-06T20:46:31.833 回答
0

您可以使用连接和列表推导来做到这一点。

def goodChars(s):
  return " ".join(["".join([y for y in x if y.isdigit() or y.isalpha()]) for x in s.split()])
于 2014-10-21T19:33:15.363 回答