Python:替换功能不起作用

时间:2017-10-27 16:18:01

标签: python

SecureRandom saltRand = new SecureRandom(new byte[] { 1, 2, 3, 4 });
byte[] salt = new byte[16];
saltRand.nextBytes(salt);

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec("password".toCharArray(), salt, 1024, 128);
SecretKey key = factory.generateSecret(spec);

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(key.getEncoded());
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128, sr);
sksCrypt = new SecretKeySpec((kg.generateKey()).getEncoded(), "AES");

嗨!我的python代码由于某种原因不起作用。我正试图摆脱任何标点符号和空白区域。

5 个答案:

答案 0 :(得分:2)

您的代码会清除以前替换的结果。

for s in str:
    for x in EXTRANEOUS:
        s = s.replace(x,"")
    l.append(s)

答案 1 :(得分:1)

只需使用Integer searchValue; if ((searchValue = checkPerfectMatch(searchTerm)) != null) { // ... } else if ((searchValue = checkGoodMatch(searchTerm)) != null) { // ... } else if ((searchValue = checkHalfMatch(searchTerm)) != null) { // ... }

re.sub

输出:

import re
str = ["HeLlo!!!,","H%I"]
final_str = [re.sub('\W+', '', i) for i in str]

答案 2 :(得分:0)

您始终使用字符串的未修改版本s。只需将sd替换为s

即可
PUNCTUATION = '''!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'''

WHITE_SPACE = ' \t\n\r\v\f'

EXTRANEOUS  = PUNCTUATION + WHITE_SPACE

str = ["HeLlo!!!,","H%I"]
l = []
for s in str:
    for x in EXTRANEOUS:
        s = s.replace(x,"")
    l.append(s)
print(l)

答案 3 :(得分:0)

正如其他人所说,你没有将s函数的输出存储在sd = s.replace(x,"")中。只需将s = s.replace(x,"")替换为l.append(s)Python 2.x即可。删除标点符号的另一种方法是这样做。

<强> import string stri = ["HeLlo!!!,","H%I"] l = [] for s in stri: l.append(s.translate(None, string.punctuation)) print(l)

Python 3.x

<强> import string stri = ["HeLlo!!!,","H%I"] l = [] for s in stri: l.append(s.translate(str.maketrans("","", string.punctuation))) print(l)

['HeLlo', 'HI']

输出:

{{1}}

答案 4 :(得分:0)

你可以这样做:

PUNCTUATION = '''!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'''
WHITE_SPACE = ' \t\n\r\v\f'    
EXTRANEOUS = PUNCTUATION + WHITE_SPACE    
str_list = ["HeLlo!!!","H%I"] # this a list
result = []

for s in str_list:
    l = ""
    for x in s:
        if x not in EXTRANEOUS:
            l += x
    result.append(l)

print(result)

输出:

['HeLlo', 'HI']