繁体   English   中英

Python - 如何删除某些符号后所有行中的所有字符?

[英]Python - how to delete all characters in all lines after some sign?

我想删除@符号后面所有行中的所有字符。 我写了一些代码:

#!/usr/bin/env python
import sys, re, urllib2
url = 'http://varenhor.st/wp-content/uploads/emails.txt'
document = urllib2.urlopen(url)
html = document.read()

html2 = html[0]
for x in html.rsplit('@'):
    print x

但它只删除@符号并将其余字符复制到下一行。 那么如何修改这段代码,删除@之后所有行中的所有字符? 我应该使用正则表达式吗?

你分裂的次数太多了; 请改用str.rpartition()然后忽略@之后的部分。 每行执行此操作:

for line in html.splitlines():
    cleaned = line.rpartition('@')[0]
    print cleaned

或者,对于较旧的Python版本,将str.rsplit()限制为仅1次拆分,并再次仅获取第一个结果:

for line in html.splitlines():
    cleaned = line.rsplit('@', 1)[0]
    print cleaned

无论换行样式如何,我都使用str.splitlines()来干净地分割文本。 您还可以直接遍历urllib2响应文件对象:

url = 'http://varenhor.st/wp-content/uploads/emails.txt'
document = urllib2.urlopen(url)
for line in document:
    cleaned = line.rpartition('@')[0]
    print cleaned

演示:

>>> import urllib2
>>> url = 'http://varenhor.st/wp-content/uploads/emails.txt'
>>> document = urllib2.urlopen(url)
>>> for line in document:
...     cleaned = line.rpartition('@')[0]
...     print cleaned
... 
ADAKorb...
AllisonSarahMoo...
Artemislinked...
BTBottg...
BennettLee...
Billa...
# etc.

您可以使用Python的切片表示法:

import re
import sys
import urllib2

url = 'http://varenhor.st/wp-content/uploads/emails.txt'
document = urllib2.urlopen(url)
html = document.read()

for line in html.splitlines():
    at_index = line.index('@')
    print line[:at_index]

由于字符串是序列,您可以对它们进行切片。 例如,

hello_world = 'Hello World'
hello = hello_world[:5]
world = hello_world[6:]

请记住,切片会返回一个新序列,而不会修改原始序列。

由于您已import ed re ,因此可以使用它:

document = urllib2.urlopen(url)
reg_ptn = re.compile(r'@.*')
for line in document:
    print reg_ptn.sub('', line)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM