[英]how to use enumerate with regex(findall) in python?
I have a txt file as follows, 我有一个txt文件,如下所示:
#onetwothree.txt
>one
QWERTYUIOP
>two
ASDFGHJKL
>three
ZXCVBNM
...
and I want to split that txt file into several files as follows, 我想将txt文件分成几个文件,如下所示:
#one.txt
>one
QWERTYUIOP
and 和
#two.txt
>two
ASDFGHJKL
and 和
#three.txt
>three
ZXCVBNM
here is the code I worte, 这是我编写的代码,
import re
with open("onetwothree.txt") as file:
name=re.findall(r'\>[^\n]+',file.read())
sequence=re.findall(r'name[ind][^/n]+' for ind in enumerate(name), file.read())
.
.
.
I know that there is something wrong in following part. 我知道下一部分有问题。
sequence=re.findall(r'name[ind][^/n]+' for ind in enumerate(name), file.read())
I want to make a list using re.findall
, enumerate
and following list is what I want to get. 我想使用re.findall
创建一个列表, enumerate
,下面的列表是我想要的。
>>>print (seq)
["QWERTYUIOP","ASDFGHJKL","ZXCVBNM"]
how can I fix this code sequence=re.findall(r'name[ind][^/n]+' for ind in enumerate(name), file.read())
right? 我该如何解决此代码sequence=re.findall(r'name[ind][^/n]+' for ind in enumerate(name), file.read())
?
First of all, you can't read a file twice using read()
, second time you call it, it returns an empty string. 首先,您不能使用read()
两次读取文件,第二次调用该文件时,它将返回一个空字符串。
Also, i think you got the wrong understanding of re.findall
. 另外,我认为您对re.findall
理解不re.findall
。 It takes only 2 parameters (regex,string). 它仅需要2个参数(正则表达式,字符串)。
You can accomplish the task in one go, without calling findall
twice. 您可以一次完成任务,而无需两次调用findall
。
s = '''>one
QWERTYUIOP
>two
ASDFGHJKL
>three
ZXCVBNM
''' # replace this with file.read()
res = re.findall(">([^\n]+)\n(\w+)",s) #each regex in paren constitutes a group
print(res)
#[('one ', 'QWERTYUIOP'), ('two', 'ASDFGHJKL'), ('three', 'ZXCVBNM')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.