www.Linux-Support.com

  • Increase font size
  • Default font size
  • Decrease font size
Home

Parsing Lists of Integers with Python

Print
Article Index
1. Parsing Strings

python-appParsing configuration files and user input is a quite common task. The present recipe provides an algorithm to parse a list of integers and ranges of integers. Optional parameters control how to handle parsing errors and what characters are to be used for separating elements within the input string.

1. Parsing Strings

The first part of the script contains the method for paring the input string. The method returns a list of integers. (Ranges will be converted to lists, too.)

At the bottom of the script you will find some examples, how to utilize the introduced method and how to play with the signature of the method.

# parse a string containing a list of integers
def parse_int_list(str_list, separator=',', silent=True):
""" Takes a list of elements separated by 'separator'. Parsing error will be
ignored if 'silent' is True, else an Exception is raised.
Parameters:
str_list
Elements in this string are separated by 'separator'. Elements are ingle
integers or ranges of integers, for example '1,2,3,10-19,100,200-250'.
separator
Defines the separator to be utilized to split the input string. 'separator'
defaults to ','.
silent
If silent equals True, then all parsing errors will be ignored. Else an
exception will be thrown."""

 
# remove white space
str_list = "".join( str_list.split() )
aset = set()
for element in str_list.split(separator):
alist = element.split('-')
if len(alist) not in [1,2] and not silent:
raise SyntaxError("incorrect format")
 
if len(alist) == 1:
try:
aset.add(int(alist[0]))
except Exception, e:
if not silent:
raise e
elif len(alist) == 2:
try:
aset.update( set(range(int(alist[0]),int(alist[1])+1)) )
except Exception, e:
if not silent:
raise e
ret = list(aset)
ret.sort()
return ret
 
# test the method for parsing strings
print( parse_int_list('1,2,3,4,5-7,100-105') )
#output: [1, 2, 3, 4, 5, 6, 7, 100, 101, 102, 103, 104, 105]
 
print( parse_int_list('1,2,a,b,c,900-909,a-c-d') )
#output: [1, 2, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909]
 
print( parse_int_list('1;2;5-8', separator=';') )
#output: [1, 2, 5, 6, 7, 8]
 
print( parse_int_list('1,3,1-2-3', silent=False) )
#output: ValueError: invalid literal for int() with base 10: 'x'
 
print( parse_int_list('1,3,x', silent=False) )
#output: SyntaxError: incorrect format
 

Last Updated on Saturday, 10 July 2010 23:29