gpt4 book ai didi

python - 匹配一行,追加在它下面

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:31 24 4
gpt4 key购买 nike

我正在尝试编辑文件的格式,不对,它看起来像这样:

>Cluster 0
L07510
>Cluster 1
AF480591
AY457083
>Cluster 2
M88154
>Cluster 3
CP000924
L09161
>Cluster 4
AY742307
>Cluster 5
L09163
L09162
>Cluster 6
AF321086
>Cluster 7
DQ666175
>Cluster 8
DQ288691

我想用 python 写一些东西,它会遍历每一行,停在说“>Cluster x”(x 是一个数字)的行,然后将该数字添加到它后面的任何行。然后,当到达新的“>Cluster x”时,它会以新的 x 值重新开始。

所以它看起来像这样:

>Cluster 0
0 L07510
>Cluster 1
1 AF480591
1 AY457083
>Cluster 2
2 M88154
>Cluster 3
3 CP000924
3 L09161
>Cluster 4
4 AY742307
>Cluster 5
5 L09163
5 L09162
>Cluster 6
6 AF321086
>Cluster 7
7 DQ666175
>Cluster 8
8 DQ288691

我想我可以使用 regex,搜索 ">Cluster x"(regex 看起来像这样吗?('\>Cluster\d+')) 然后让程序在这个匹配的正则表达式之后的每一行附加 \d+ 是什么。我只是不确定如何实际写这个。任何帮助将不胜感激!

最佳答案

测试

# If you're on a POSIX compliant system, and this script is marked as 
# executable, the following line will make this file be automatically
# run by the Python interpreter rather than interpreted as a shell script
#!/usr/bin/env python

# We need the sys module to read arguments from the terminal
import sys

# Open the input file, default mode is 'r', readonly, which is a safe default
infile = open(sys.argv[1])

# Prepare a variable for the cluster number to be used within the loop
cluster = ''

# loop through all lines in the file, but first set up a list comprehension
# that strips the newline character off the line for each line that is read
for line in (line.strip() for line in infile):
if line.startswith('>'):
# string.split() splits on whitespace by default
# we want the cluster number at index 1
cluster = line.split()[1]

# output this line to stdout unmodified
print line

else:
# output any other line modified by adding the cluster number
print cluster + ' ' + line

用法

$ python cluster_format.py input.txt > output.txt

关于python - 匹配一行,追加在它下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17453733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com