gpt4 book ai didi

python - 从字符串中删除符号

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:59 24 4
gpt4 key购买 nike

我有一个文件,例如:

@HWI
ABCDE
+
@HWI7
EFSA
+
???=AF
GTEY@JF
GVTAWM

我只想保留字符串(因此删除包含符号的所有内容)

我尝试过:

import numpy as np
arr = np.genfromtxt(f, dtype=str)

for line in np.nditer(arr):
if np.core.defchararray.isupper(line) and not '@?=;?+' in line:
print line

但它给出:

@HWI
ABCDE
@HWI7
EFSA
???=AF
GTEY@JF
GVTAWM

我期待:

ABCDE
EFSA
GVTAWM

我想为此使用 numpy,而不是像 regex 或类似的命令。

最佳答案

这是我的解决方案:

import numpy as np

arr = np.genfromtxt('text.txt', dtype=str)

test = np.core.defchararray.isalpha(arr) #Create a mask : True = only str and False = not only str

print arr[test] #Use the mask on arr and it will print only good values

不要将 if 与 numpy 一起使用!您可以通过索引来做到这一点;)

我明白了:

['ABCDE' 'EFSA' 'GVTAWM']

关于python - 从字符串中删除符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com