python - 如何在嵌套的 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'))
解决方案
推荐阅读
- python - 我正在尝试复制 html 网站,但每当我尝试时,它只会给我其中的一部分
- android - 如何通过 curl POST 命令发送 logcat
- python - Python在CSV文件中重命名具有匹配名称的文件夹中的文件
- excel - Excel PowerQuery - 自定义函数/查询以从文件中获取单元格值
- windows - MinWG 中对 _imp_* 的未定义引用
- layout - SwiperJS 通过使用 GRID 被粉碎
- python - pip 安装自定义 git 存储库,未应用更改
- reactjs - 如何在生产环境中查看 Create React App 中的调试日志?
- sql - 在spark sql中将字符串转换为日期时获取null
- sql - 如何在 SQLite 中创建继承