Python 如何从';从'开始;直到';结束于';
我喜欢将原始文本文件的某些部分保存到新的文本文件中,这些部分可以在“startswith”和“endswith”字符串之间识别 示例:输入文本文件包含以下行:Python 如何从';从'开始;直到';结束于';,python,python-3.x,startswith,ends-with,Python,Python 3.x,Startswith,Ends With,我喜欢将原始文本文件的某些部分保存到新的文本文件中,这些部分可以在“startswith”和“endswith”字符串之间识别 示例:输入文本文件包含以下行: ...abc… ...starts with string... ...def... ...ends with string... ...ghi... ...jkl... ...starts with string... ...mno... ...ends with string... ...pqr... 我有兴趣将以下行提取到输出文
...abc…
...starts with string...
...def...
...ends with string...
...ghi...
...jkl...
...starts with string...
...mno...
...ends with string...
...pqr...
我有兴趣将以下行提取到输出文本文件中:
starts with string...def...ends with string
starts with string...mno...ends with string
下面的代码返回空列表[]。请帮我更正代码
with open('file_in.txt','r') as fi:
id = []
for ln in fi:
if ln.startswith("start with string"):
if ln.endswith("ends with string"):
id.append(ln[:])
with open(file_out.txt, 'a', encoding='utf-8') as fo:
fo.write (",".join(id))
print(id)
我希望file.out.txt包含以“以字符串开头”和“以字符串结尾”开头的所有字符串。并返回True或False,而不是可以用来分割字符串的位置。试试看,或者相反。例如:
start = 'starts with string'
end = 'ends with string'
s = '...abc… ...starts with string... ...def... ...ends with string... ...ghi...'
sub = s[s.find(start):s.find(end) + len(end)]
print(sub)
# starts with string... ...def... ...ends with string
您需要在循环中添加一些检查,以查看开始字符串和结束字符串是否存在,因为如果不存在匹配项,
find
将返回-1,这将导致一些意外的切片。您可以使用一个单独的变量来指示当前行是否是感兴趣的部分,并根据需要切换此变量在开始和停止标记上。然后,您还可以将此功能转换为生成器:
def extract(fh, start, stop):
sub = False
for line in fh:
sub |= start in line
if sub:
yield line
sub ^= stop in line
with open('test.txt') as fh:
print(''.join(extract(fh, 'starts with string', 'ends with string')))
在Python 3.8中,您可以使用:
变化:不包括开始和停止标记
如果从输出中排除启动和停止标记,我们可以再次使用:
每行末尾都有一个字符,告诉计算机显示新行。我在这里假设“以字符串开始”和“以字符串结束”在同一行。如果不是这种情况,请在第一个If语句的正下方添加--“id.append(ln[:])” 试一试 或
感谢您更新测试数据。我已经进行了相应的更新,请检查它是否符合您的需要。@MadPhysicast我已经更新了我的答案,以满足OP的要求(在输出中包括开始和停止标记),并使用了一个示例用法。@a_guest:使用“赋值表达式”:以下错误:>文件“”,第4行,而任何(在fh中的x中从(行:=x)开始):^Syntaxer错误:无效syntax@anatta如前所述,Python3.8中引入了赋值表达式,该表达式目前仅作为@a_guest:错过了。我将更新到3.8alpha,然后再试一次。谢谢。仍然返回空列表[]。我将尝试一些变化和更新。谢谢。我有另一个使用布尔运算符的策略。请参阅更新的代码。
import itertools as it
def extract(fh, start, stop):
while any(start in (line := x) for x in fh):
yield line
yield from it.takewhile(lambda x: stop not in x, ((line := y) for y in fh))
yield line
with open('test.txt') as fh:
print(''.join(extract(fh, 'starts with string', 'ends with string')))
import itertools as it
def extract(fh, start, stop):
while any(start in x for x in fh):
yield from it.takewhile(lambda x: stop not in x, fh)
with open('test.txt') as fh:
print(''.join(extract(fh, 'starts with string', 'ends with string')))
ln.endswith("ends with string"+'\n' )
ln.endswith("ends with string"+'\n' +'\r')
with open('C:\\Py\\testing.txt','r') as fi:
id = []
x = 0
copy_line = False
for ln in fi:
if "starts with string" in ln:
copy_line = True
if copy_line:
id.append ( ln[:] )
if "ends with string" in ln :
copy_line = False
with open ('C:\\Py\\testing_out.txt', 'a', encoding='utf-8' ) as fo:
fo.write (",".join(id))
print(id)