我有一个txt文件如下
#onetwothree.txt
>one
QWERTYUIOP
>two
ASDFGHJKL
>three
ZXCVBNM
...
我想把这个txt文件分成几个文件,如下所示
#one.txt
>one
QWERTYUIOP
以及
#two.txt
>two
ASDFGHJKL
以及
#three.txt
>three
ZXCVBNM
这是我的密码
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())
.
.
.
我知道下面的部分有问题。你知道吗
sequence=re.findall(r'name[ind][^/n]+' for ind in enumerate(name), file.read())
我想用re.findall
,enumerate
做一个列表
下面的清单就是我想要的。你知道吗
>>>print (seq)
["QWERTYUIOP","ASDFGHJKL","ZXCVBNM"]
如何修复此代码sequence=re.findall(r'name[ind][^/n]+' for ind in enumerate(name), file.read())
对吗?你知道吗
首先,不能使用
read()
读取文件两次,第二次调用时,它返回一个空字符串。你知道吗另外,我认为你对
re.findall
的理解是错误的。它只需要2个参数(regex,string)。你知道吗您可以一次性完成任务,而无需调用
findall
两次。你知道吗相关问题 更多 >
编程相关推荐