Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过读取特定字符&x27;的左侧来删除行:';?_Python_Python 3.x - Fatal编程技术网

Python 通过读取特定字符&x27;的左侧来删除行:';?

Python 通过读取特定字符&x27;的左侧来删除行:';?,python,python-3.x,Python,Python 3.x,我有一个100行的文本文件,如下所示: hsh:222 shhhshshs:2294 sjasda:2324 s_s:223 aaa:111 asdasd:1111 for x in f: newline = x.strip()+.split(':') if len(newline.rstrip()) >= 3: f2.write(newline) 我想删除冒号前超过3个字符的所有行,以便输出为: hsh:222 s_s:223 aaa:111 我该怎

我有一个100行的文本文件,如下所示:

hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
for x in f:
    newline = x.strip()+.split(':')
    if len(newline.rstrip()) >= 3:
        f2.write(newline)
我想删除冒号前超过3个字符的所有行,以便输出为:

hsh:222
s_s:223
aaa:111
我该怎么做?我不知道如何在冒号前向左读。我假设是这样的:

hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
for x in f:
    newline = x.strip()+.split(':')
    if len(newline.rstrip()) >= 3:
        f2.write(newline)

我会利用列表理解进行过滤。您可以使用
str
类的
find()
方法来查找每个单词中冒号的索引,然后将单词从开头分割到冒号索引

list_ = ['hsh:222', 'shhhshshs:2294', 'sjasda:2324', 's_s:223', 'aaa:111', 'asdasd:1111']
output = [word for word in list_ if len(word[:word.find(':')]) <= 3]
# output content: ['hsh:222', 's_s:223', 'aaa:111']

你快到了。您可以直接对方法的结果调用方法:

elements = x.strip().split(':')
这将返回两个元素的列表。要获取列表的第一个元素,请为其编制索引:

first = elements[0]
或作为一个班轮:

first = x.strip().split(':')[0]

那就照常进行吧。请记住,如果len(首先)我必须对我的答案进行一些编辑,那么您应该将
x
写入输出
。您可以尝试使用这两行获得相同的结果。假设每行至少有2个字符

with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if x[3] == ':': f2.write(x)
下面的代码将解决这个假设。无论每行的长度如何,它都应该能够读取文件

import re
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if re.search('^...:',x): f2.write(x)
reg表达式将检查x的值是否以任何3个字符开头,后跟冒号(:)。如果是,则该行已准备好写入文件

我在xyz.txt中的输入文件有以下记录

hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
a:
hsh:222
s_s:223
aaa:111
out.txt中的outut文件具有以下记录

hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
a:
hsh:222
s_s:223
aaa:111
如果要检查以下模式中的任何字符串:
:123
a:123
aa:123
aaa:123

如果
a
可以是任何字符,则可以按如下方式更改代码:

print(*output, file=f2, sep='\n')
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if ':' in x[:3] : f2.write(x)
import re
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if re.search('^.{0,3}:',x): f2.write(x)
hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
a:
:1
hsh:222
s_s:223
aaa:111
a:
:1
在这里,它将检查前4个位置的

您可以使用正则表达式编写,如下所示:

print(*output, file=f2, sep='\n')
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if ':' in x[:3] : f2.write(x)
import re
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if re.search('^.{0,3}:',x): f2.write(x)
hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
a:
:1
hsh:222
s_s:223
aaa:111
a:
:1
如果输入文件如下所示:

print(*output, file=f2, sep='\n')
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if ':' in x[:3] : f2.write(x)
import re
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if re.search('^.{0,3}:',x): f2.write(x)
hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
a:
:1
hsh:222
s_s:223
aaa:111
a:
:1
输出结果如下:

print(*output, file=f2, sep='\n')
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if ':' in x[:3] : f2.write(x)
import re
with open("xyz.txt", "r") as f, open("out.txt", 'w') as f2:
    for x in f:
        if re.search('^.{0,3}:',x): f2.write(x)
hsh:222
shhhshshs:2294
sjasda:2324
s_s:223
aaa:111
asdasd:1111
a:
:1
hsh:222
s_s:223
aaa:111
a:
:1

newline=x.strip()+.split(“:”)
应该做什么?查看这些函数的文档并正确调用它们(例如,删除
+
)难道你不能只检查
如果x[4]=':':f2。写(x)
你也可以使用if语句作为
如果word[4]=':'
哦,是的,的确如此!那会更干净。谢谢你指出这一点@JoeFerndzOP允许少于3个字符。“聪明的方法,tho!”物理学家,谢谢你的留言。我用正则表达式搜索:第4位。请检查并让我知道是否需要。看到任何挑战。用这种方法。感谢你的意见,并敦促我给出一个简单的答案。我认为你没有抓住要点。首先,第一个假设中的假设至少是4个字符,而不是2个字符。其次,您应该在x[:3]
中测试“:”是否正确。正则表达式应该是
^.{1,3}:
如果你使用
re.match
你不需要
^
如果我不使用^。例如,如果字符串为
abcd:123
,则它也会选择该字符串。