Dear Friends,
If you need to add random integer to all values in a text, this is what you can do. Especially useful if you need to fake a report.
import subprocess
import fileinput
import sys
from random import randint
open('mytext.txt', 'w').close()
subprocess.call(['notepad.exe', 'mytext.txt'])
for line in fileinput.input('mytext.txt', inplace=True):
# get all numbers from row
numbers = [int(x) for x in line.strip().split()]
if numbers[0] < 999:
numbers = [x + randint(25,101) for x in numbers]
else:
numbers = [x + randint(50,500) for x in numbers]
# re-map modified numbers to a line of text
sys.stdout.write(' '.join([str(x) for x in numbers]) + '\n')
subprocess.call(['notepad.exe', 'mytext.txt'])
==============================================
Notes:
1. This add random number between 25 and 101 if any value is less than 999, else it adds random number between 50 and 500.
2. Running this on linux will need to remove subprocess call, since linux will not have notepad.exe. If that is the case, you can directly modify file without opening.
If you need to add random integer to all values in a text, this is what you can do. Especially useful if you need to fake a report.
import subprocess
import fileinput
import sys
from random import randint
open('mytext.txt', 'w').close()
subprocess.call(['notepad.exe', 'mytext.txt'])
for line in fileinput.input('mytext.txt', inplace=True):
# get all numbers from row
numbers = [int(x) for x in line.strip().split()]
if numbers[0] < 999:
numbers = [x + randint(25,101) for x in numbers]
else:
numbers = [x + randint(50,500) for x in numbers]
# re-map modified numbers to a line of text
sys.stdout.write(' '.join([str(x) for x in numbers]) + '\n')
subprocess.call(['notepad.exe', 'mytext.txt'])
==============================================
Notes:
1. This add random number between 25 and 101 if any value is less than 999, else it adds random number between 50 and 500.
2. Running this on linux will need to remove subprocess call, since linux will not have notepad.exe. If that is the case, you can directly modify file without opening.
Comments
Post a Comment