我在列表中有成千上万个句子元素。但是,许多句子不可读。因此,我想在替换过程中忽略这些句子。幸运的是,这些句子具有相似的模式。
已更新
这是我的示例列表:
sentence = ['"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb\n',
'"// How are you? \n',
'//# Hello world \n',
'// sourceMappingURL=data:application/json;charset=utf-8;base64,asdhhi3242jlfaoiuq3r'
]
我想在所有句子中用单个空格/
代替#
,\n
,' '
等符号,但不可读的元素除外。不可读元素始终具有包含sourceMappingURL=data:application/json;charset=utf-8;base64
的模式。
这是我使用Python 3的代码
text = []
for i in sentence:
isbase64 = "sourceMappingURL=data:application/json;charset=utf-8;base64"
if i.find(isbase64):
pass
else:
word = i.replace("\n"," ").replace("/"," ").replace("#"," ")
s = ' '.join(word)
text.append(s)
但是输出是empty list
。我有什么需要改变的吗?
预期输出:
text = ['How are you?','Hello world']
谢谢您的帮助。
答案 0 :(得分:1)
使用列表理解:
sentence = ['"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb\n',
'"// How are you? \n',
'//# Hello world \n',
'// sourceMappingURL=data:application/json;charset=utf-8;base64,asdhhi3242jlfaoiuq3r'
]
print([x.replace("\n","").replace("/","").replace("#","").strip() for x in sentence if 'sourceMappingURL=data:application/json;charset=utf-8;base64' not in x])
以上代码是以下内容的全面版本:
text = []
for x in sentence:
if 'sourceMappingURL=data:application/json;charset=utf-8;base64' not in x:
text.append(x.replace("\n","").replace("/","").replace("#","").strip())
print(text)
答案 1 :(得分:1)
您的代码看起来大部分都可以正常工作-您刚刚输入了错字!
当句子部分包含isbase64
时,您用文本...charset:utf...
定义了...charset=utf...
。
答案 2 :(得分:1)
您输入错了:
sourceMappingURL=data:application/json;charset:utf-8;base64
应该是
sourceMappingURL=data:application/json;charset=utf-8;base64
和 =
sentence = ['"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb\n',
'"// How are you? \n',
'//# Hello world \n',
'// sourceMappingURL=data:application/json;charset=utf-8;base64,asdhhi3242jlfaoiuq3r'
]
text = []
isbase64 = "sourceMappingURL=data:application/json;charset=utf-8;base64"
for i in sentence:
if isbase64 in i:
pass
else:
word = i.replace("\n"," ").replace("/"," ").replace("#"," ")
s = ' '.join(word)
text.append(s)
print text
并使用更多的pythonic方法if string in otherString
干杯:)
答案 3 :(得分:1)
i.find(isbase64)
返回的是整数而不是布尔值。
如果我确实包含isbase64,它将返回位置(例如,在第一种情况下为5),否则返回-1(例如,在第二个字符串中)。
如果将if替换为:i.find(isbase64)!=-1
,它将正常工作(或在i中使用isbase64)。
编辑:您还需要用isbase64 = "sourceMappingURL=data:application/json;charset=utf-8;base64"
将isbase64 =替换为该行(您在那里有错字)
Edit2:您还需要打印text
才能查看结果,即print(text)