简体   繁体   English

如何使用Python用随机数替换文件中的数字?

[英]How to replace numbers in file with random ones using Python?

I have a file that contains numbers in this format: 我有一个文件,其中包含以下格式的数字:

78 23 69 26 56 59 74 45 94 28 37 
62 52 84 27 12 95 86 86 12 89 92
43 84 88 22 31 25 80 40 59 32 98

(All the numbers are on a single wrapped line in notepad++, and it contains 1,5k of 2 digit number sets, with space in between) (所有数字都在notepad ++中的单行中,并且包含1,5k的2位数字集,中间有空格)

What I want to do is randomize some of the numbers everytime I run the Python code, so the second .tmp file will be unique, but keeping the same format. 我想做的是每次我运行Python代码时随机分配一些数字,因此第二个.tmp文件将是唯一的,但保持相同的格式。

So I tried this and worked, but using static numbers: 12 as search and 55 as target. 因此,我尝试了此方法并工作了,但是使用了静态数:将12用作搜索,将55用作目标。

infile = open('file.txt', 'r')
outfile = open('file.txt.tmp', 'w')
for line in infile:
    outfile.write(line.replace('12', '55'))

infile.colse()
outfile.colse()

However, for a better randomization, what I want to do is to use random numbers between 10-99 instead of static numbers like 12 and 55. 但是,为了获得更好的随机性,我想做的是使用10-99之间的随机数,而不是12和55这样的静态数。

So what I tried to do (and failed) is replacing the static 12 and 55 numbers to randomized ones like that: 所以我想做的(失败了)是将静态的12和55替换为随机数,例如:

randnum1 = randint(10,99)
randnum2 = randint(10,99)

infile = open('file.txt', 'r')
outfile = open('file.txt.tmp', 'w')
for line in infile:
    outfile.write(line.replace(randnum1, randnum2))

And I get this error: 我得到这个错误:

Traceback (most recent call last):
  File "<pyshell#579>", line 2, in <module>
    outfile.write(line.replace(randnum1, randnum2))
TypeError: Can't convert 'int' object to str implicitly

randint gives an int , which needs to be converted to str . randint给出一个int ,需要将其转换为str

Try outfile.write(line.replace(str(randnum1), str(randnum2))) 试试outfile.write(line.replace(str(randnum1), str(randnum2)))

As simple as that :) 就如此容易 :)

The error exactly says what the problem is: TypeError: Can't convert 'int' object to str implicitly . 该错误正好说明了问题所在: TypeError: Can't convert 'int' object to str implicitly It is issued because randnum1 and randnum2 are int s and not str s. 发出该消息是因为randnum1randnum2int而不是str

You must convert them to str by calling str(randnum1) and str(randnum2) , eg like this: 您必须通过调用str(randnum1)str(randnum2)将它们转换为str ,例如:

randnum1 = randint(10,99)
randnum2 = randint(10,99)
randnum1 = str(randnum1)
randnum2 = str(randnum2)

infile = open('file.txt', 'r')
outfile = open('file.txt.tmp', 'w')
for line in infile:
    outfile.write(line.replace(randnum1, randnum2))

Note: it is not recommended to use one variable name multiple times with multiple value types as it makes the code less readable. 注意:建议不要将一个变量名与多个值类型一起使用多次,因为这样会使代码的可读性降低。 However, in this case, it is re-used once so it does not harm the readability very much. 但是,在这种情况下,它只能重复使用一次,因此不会对可读性造成很大的损害。

Just in case you find it useful, you could take the following approach. 万一您发现它有用,可以采用以下方法。 This first reads your single line in and splits it into a list. 这首先读取您的单行并将其拆分为列表。 It then picks 10 random entries from the list and replaces the entry with a new random number between 10 and 99 . 然后,它从列表中选择10随机条目,并将条目替换为介于1099之间的新随机数。 Lastly it writes the new data back to the file. 最后,它将新数据写回到文件中。

from random import randint

with open('input.txt') as f_input:
    data = f_input.readline().split()
    entries = len(data) - 1

for _ in xrange(10):
    data[randint(0, entries)] = str(randint(10, 99))

with open('input.txt', 'w') as f_output:
    f_output.write(' '.join(data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM