我希望能够将相同的字符串集替换为随机的新100.
出发地:
color = { 0 0 0 }
致:
color = { 2 5 10 }
color = { 1 59 102 }
color = { 9 72 200 }
在文本文件的每一行上.
以下是我的python代码.
import random
for i in range(0, 10):
x = str(random.randrange(0, 256))
y = str(random.randrange(0, 256))
z = str(random.randrange(0, 256))
with open("9.txt", "r") as file:
file_data = file.read()
file_data = file_data.replace("color = { 0 0 0 }", "color = { " + x + " " + y + " " + z + " }")
with open("colored.txt", "w") as file:
file.write(file_data)
此代码全部正确更改,但更改为相同的值,并且我希望它是唯一的数字.