1

私はpythonで単語を感知するプログラムに取り組んでいます。ユーザー入力 = 私のメール ID は harry@hogwarts.com 出力 = 私のメール ID は xxxxx@hogwarts.com

これは私がこれまでに持っているものです

def main():
    message = []
    userInput = str(input("Enter the sentence: "))
    splitInput = str(list(userInput))
    print(splitInput)
    for item in splitInput:
        indeces = splitInput.index('@')
        while((indeces-1).isalpha()):
            item = 'x'
            message.append(item)
    print(' '.join(message))

これは私が得るエラーです

File "C:\Users\Manmohit\Desktop\purifier.py", line 8, in main
    while((indeces-1).isalpha()):
AttributeError: 'int' object has no attribute 'isalpha'

私はさまざまな方法をオンラインで探してみました。私はアルファメソッドに似たものが欲しいです。チェックするために独自のアルファメソッドを作成する必要がありますか、それとも組み込みのものを使用できますか??? 助けていただければ幸いです。ありがとうございました

更新:

ループwhile((indeces-1).isalpha()):を変更するwhile((str(indeces-1)).isalpha()):と、エラーは発生しませんが、出力も得られません。

4

3 に答える 3

1

この関数を使用して電子メールをエンコードできます。

>>> def encodeemail(email):
       e = email.split("@")
       return "@".join(["x" * len(e[0]), e[1]])

>>> encodeemail("harry@hogwarts.com")
xxxxx@hogwarts.com

あるいは

>>> def encodeemail(email):
        d = email.split(" ")
        for i, f in enumerate(d):
            e = f.split("@")
            if len(e) > 1: d[i] = "@".join(["x" * len(e[0]), e[1]])
    return " ".join(d)

>>> encodeemail("this is harry@hogwarts.com")
this is xxxxx@hogwarts.com

列挙なし:

>>> def encodeemail(email):
        d = email.split(" ")
        for i in range(len(d)):
            e = d[i].split("@")
            if len(e) > 1: d[i] = "@".join(["x" * len(e[0]), e[1]])
    return " ".join(d)
于 2013-07-28T18:12:17.677 に答える
0

re文字列に電子メールのみが含まれていない場合は、モジュールを使用できます。

>>> s
'my email id is harry@hogwards.com'
>>> re.sub('(?<=\s)\w+(?=@)', lambda y: 'x'*len(y.group()), s)
'my email id is xxxxx@hogwards.com'
于 2013-07-28T18:23:33.747 に答える
0

文字列の一部が isalpha() テストに合格するか不合格になるかを確認したいと思います。それはあなたのコードが行っていることではありません。

問題を解決するためのより良い方法があるかもしれませんが、コードを機能させることもできます。うまくいけば、それは役に立ちます。

while((indeces-1).isalpha()):

indeces は -1 と同様に整数であるため、 isalpha を適用した結果は、エラーが示すように int になります。

(str(indeces-1)).isalpha()

これもうまくいきません。これは int から文字列を作成しているため、str(2) の結果は "2" になり、これは必要なテストでもありません。

文字をチェックしたい場合は、次のように文字列にインデックスを付けるだけです。

>>> for i in range(len(s)):
...    if s[i].isalpha():
...         print 'x'
...    else:
...         print s[i]
于 2013-07-28T19:09:13.530 に答える