I want to get a random value from .ini file in the range of 14, 55. I know I'm doing it wrong but what is the right way?
import random
import configparser
IN_settings = configparser.ConfigParser()
IN_settings.read('config.ini')
print(IN_settings['common'].getint((random.randint('out_put')))
and this is my config file:
[common]
out_put = 14, 55
The random.randint()
function does not take a string as an argument. It takes two integers as arguments.
So, when you say random.randint('out_put')
, the function does not work. 'out_put'
is a string, not two integers.
You will need to read in the actual integers first. Of course, the field out_put
is not a single integer, but rather two integers. We'll have to read it as a string, and then convert it.
range_str = IN_settings['common'].get('out_put')
range_ints = [int(x) for x in range_str.split(',')]
random_int = random.randint(range_ints[0], range_ints[1])
print(random_int)
That works. For your example file, this will print a random integer in the interval [14, 55] (including the end points).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.