我想删除句子中的所有网址。
\n这是我的代码:
\nimport ijson\nf = open("/content/drive/My Drive/PTT \xe7\x88\xac\xe8\x9f\xb2/content/MakeUp/PTT_MakeUp_content_0_1000.json")\nobjects = ijson.items(f, \'item\')\n\nfor obj in list(objects):\n article = obj[\'content\']\n ret = re.findall("http[s*]:[a-zA-Z0-9_.+-/#~]+ ", article) # Question here\n for r in ret:\n article = article.replace(r, "")\n print(article)\nRun Code Online (Sandbox Code Playgroud)\n但句子中仍然留下了一个带有“http”的URL。
\narticle_example = "\xe7\x9c\xbc\xe5\xbd\xb1\xe7\x9b\xa4\xe9\x95\xb7\xe9\x80\x99\xe6\xa8\xa3 http://i.imgur.com/uxvRo3h.jpg \xe8\xaa\xaa\xe7\x9c\x9f\xe7\x9a\x84 \xe5\xbe\x88\xe4\xb8\x8d\xe5\xa5\xbd\xe6\x8b\x8d"\nRun Code Online (Sandbox Code Playgroud)\n我该如何修复它?
\n一个简单的修复方法是将模式替换https?://\\S+为空字符串:
article_example = "\xe7\x9c\xbc\xe5\xbd\xb1\xe7\x9b\xa4\xe9\x95\xb7\xe9\x80\x99\xe6\xa8\xa3 http://i.imgur.com/uxvRo3h.jpg \xe8\xaa\xaa\xe7\x9c\x9f\xe7\x9a\x84 \xe5\xbe\x88\xe4\xb8\x8d\xe5\xa5\xbd\xe6\x8b\x8d"\noutput = re.sub(r'https?://\\S+', '', article_example)\nprint(output)\nRun Code Online (Sandbox Code Playgroud)\n这打印:
\n\xe7\x9c\xbc\xe5\xbd\xb1\xe7\x9b\xa4\xe9\x95\xb7\xe9\x80\x99\xe6\xa8\xa3 \xe8\xaa\xaa\xe7\x9c\x9f\xe7\x9a\x84 \xe5\xbe\x88\xe4\xb8\x8d\xe5\xa5\xbd\xe6\x8b\x8d\nRun Code Online (Sandbox Code Playgroud)\n我的模式假设 URL 后面http://或https://属于 URL 的一部分的任何非空白字符。
| 归档时间: |
|
| 查看次数: |
6359 次 |
| 最近记录: |