首页 > 解决方案 > 如何在嵌套的 while 循环和计数器的帮助下清空字符串并重新生成它

问题描述

我想清空一个字符串并想在嵌套的while循环和计数器的帮助下重新生成原始字符串。

我有一个包含数千个类似代码但不按顺序排列的大型文本文件(code.txt),但例如假设文件中的代码数仅为 7

假设我的 txt 文件包含

xx01 xx02 xx03 xx03 xx03 xx03 xx03 xx01 xx04 xx04 xx02 xx04 xx04 xx04 xx02 xx03 xx04 xx07  xx05 xx06 xx07 xx04 xx05 xx06 xx07

首先,我将用“Y”替换任何代码之一。

例如xx03 = Y(它只会转换单个代码,并非所有 xx03 都会变成 Y),我们必须选择第一个代码,否则它无法返回原始字符串。

现在我的文件(code.txt)变成或看起来像

xx01 xx02 Y xx03 xx03 xx03 xx03 xx01 xx04 xx04 xx02 xx04 xx04 xx04 xx02 xx03 xx04 xx07  xx05 xx06 xx07 xx04 xx05 xx06 xx07

然后我想使用一个嵌套的while循环(一个代码每个循环7个循环),其中一个计数器开始选择值'Y'。

所以在所有操作之后我们会得到两个文件,在第一个文件中字符串将变成 (code.txt) =xx01 xx02 Y而在第二个文件中 (count.txt) 看起来像

loop number, count, for Example 3, 4, 1, 1, 4, 2, 2, 1, 4, 3, 2, 1, 3, 1, 4, 1, 7, 1,... 

我尝试了代码,但它当然不起作用,有人可以正确修改/创建它,以便它可以正常工作。


with open('code.txt', 'r') as f:
    data = f.read()
count = 0
word = 'y' # start with picking the Y or pointer at Y in the (code.txt)
while word = ' y xx01 '
    data = data.replace(' y xx01 , ' y ', 1)
    count = count.word(data)
    count += 1
    data =  +[1]
    print("loop 1", count, file=open('count.txt', 'a'))
    print(data, file=open('code.txt', 'a'))
    count = 0
    while word = ' y xx02 '
        data = data.replace(' y xx02 , ' y ', 1)
        count = count.word(data)
        count += 1
        data =  +[1]
        print("loop 2", count, file=open('count.txt', 'a'))
        print(data, file=open('code.txt', 'a'))
        count = 0
        while word = ' y xx03 '
            data = data.replace(' y xx03 , ' y ', 1)
            count = count.word(data)
            count = count.word(data)
            data =  +[1]
            print("loop 3", count, file=open('count.txt', 'a'))
            print(data, file=open('code.txt', 'a'))
            count = 0
            while word = ' y xx04 '
                data = data.replace(' y xx04 , 'y', 1)
                count = count.word(data)
                count += 1
                data = +[1]
                print("loop 4", count, file=open('count.txt', 'a'))
                print(data, file=open('code.txt', 'a'))
                count = 0
                while word = ' y xx05 '
                    data = data.replace(' y xx05 , 'y', 1)
                    count = count.word(data) 
                    count += 1d(data)
                    data = +[1]
                    print("loop 5", count, file=open('count.txt', 'a'))
                    print(data, file=open('code.txt', 'a'))
                    count = 0
                    while word = ' y xx06 '
                        data = data.replace(' y xx06 , 'y', 1)
                        count = count.word(data)
                        count += 1
                        data = +[1]
                        print("loop 6", count, file=open('count.txt', 'a'))
                        print(data, file=open('code.txt', 'a'))
                        count = 0
                        while word = ' y xx07 '
                            data = data.replace(' y xx07 , 'y', 1)
                            count = count.word(data)
                            count += 1
                            data = +[1]
                            print("loop 7", count, file=open('count.txt', 'a'))
                            print(data, file=open('code.txt', 'a'))

标签: pythonwhile-loopcounternested-loops

解决方案


推荐阅读