Python string.split() Examples

The following are 30 code examples for showing how to use string.split(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module string , or try the search function .

Example 1
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _initializePOSTables():
    global _POSNormalizationTable, _POStoDictionaryTable
    _POSNormalizationTable = {}
    _POStoDictionaryTable = {}
    for pos, abbreviations in (
	    (NOUN, "noun n n."),
	    (VERB, "verb v v."),
	    (ADJECTIVE, "adjective adj adj. a s"),
	    (ADVERB, "adverb adv adv. r")):
	tokens = string.split(abbreviations)
	for token in tokens:
	    _POSNormalizationTable[token] = pos
	    _POSNormalizationTable[string.upper(token)] = pos
    for dict in Dictionaries:
	_POSNormalizationTable[dict] = dict.pos
	_POStoDictionaryTable[dict.pos] = dict 
Example 2
Project: razzy-spinner   Author: rafasashi   File: toolbox.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _parse_record(s):
    """
    Deprecated: use C{StandardFormat.fields()}
    
    @param s: toolbox record as a string
    @type  s: L{string}
    @rtype: iterator over L{list(string)}
    """

    s = "\n" + s                                         # Fields (even first) must start w/ a carriage return 
    if s.endswith("\n") : s = s[:-1]                     # Remove single extra carriage return
    for field in split(s, sep="\n\\")[1:] :              # Parse by carriage return followed by backslash
        parsed_field = split(field, sep=" ", maxsplit=1) # Split properly delineated field
        try :
            yield (parsed_field[0], parsed_field[1])
        except IndexError :
            yield (parsed_field[0], '') 
Example 3
Project: razzy-spinner   Author: rafasashi   File: __init__.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _substitute(self, str):
        """
        Substitute words in the string, according to the specified reflections,
        e.g. "I'm" -> "you are"
        
        @type str: C{string}
        @param str: The string to be mapped
        @rtype: C{string}
        """

        words = ""
        for word in string.split(string.lower(str)):
            if self._reflections.has_key(word):
                word = self._reflections[word]
            words += ' ' + word
        return words 
Example 4
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _initializePOSTables():
    global _POSNormalizationTable, _POStoDictionaryTable
    _POSNormalizationTable = {}
    _POStoDictionaryTable = {}
    for pos, abbreviations in (
	    (NOUN, "noun n n."),
	    (VERB, "verb v v."),
	    (ADJECTIVE, "adjective adj adj. a s"),
	    (ADVERB, "adverb adv adv. r")):
	tokens = string.split(abbreviations)
	for token in tokens:
	    _POSNormalizationTable[token] = pos
	    _POSNormalizationTable[string.upper(token)] = pos
    for dict in Dictionaries:
	_POSNormalizationTable[dict] = dict.pos
	_POStoDictionaryTable[dict.pos] = dict 
Example 5
Project: yang-explorer   Author: CiscoDevNet   File: cxml.py    License: Apache License 2.0 6 votes vote down vote up
def type_identityref_values(self, t):
        base_idn = t.search_one('base')
        if base_idn:
            # identity has a base
            idn_key = None
            base_idns = base_idn.arg.split(':')
            if len(base_idns) > 1:
                for module in self.module_prefixes:
                    if base_idns[0] == self.module_prefixes[module][0]:
                        idn_key = module + ':' + base_idns[1]
                        break
            else:
                idn_key = self.name + ':' + base_idn.arg

            if idn_key is None:
                return ''

            value_stmts = []
            stmts = self.identity_deps.get(idn_key, [])
            for value in stmts:
                ids = value.split(':')
                value_stmts.append(self.module_prefixes[ids[0]][0] + ':' + ids[1])
            if stmts:
                return '|'.join(sorted(value_stmts))
        return '' 
Example 6
Project: me-ica   Author: ME-ICA   File: meica.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def version_checker(cur_ver,ref_ver):
	"""
	Checks version in major/minor format of a current version versus a reference version.
	Supports 2 or more level versioning, only 2 levels checked)

	Input:
	cur_ver: float or string of version to check
	ref_ver: float or string of reference version

	Returns:
	bool for pass or fail
	"""
	cur_ver = str(cur_ver)
	ref_ver = str(ref_ver)
	cur_Varr = [int(vvv) for vvv in cur_ver.split('.')[0:2]]
	cur_V = cur_Varr[0]*10**2 + cur_Varr[1]%100
	ref_Varr = [int(vvv) for vvv in ref_ver.split('.')[0:2]]
	ref_V = ref_Varr[0]*10**2 + ref_Varr[1]%100
	if cur_V > ref_V: return True
	return False

#Run dependency check 
Example 7
Project: me-ica   Author: ME-ICA   File: meica_gui.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def version_checker(cur_ver,ref_ver):
	"""
	Checks version in major/minor format of a current version versus a reference version.
	Supports 2 or more level versioning, only 2 levels checked)

	Input:
	cur_ver: float or string of version to check
	ref_ver: float or string of reference version

	Returns:
	bool for pass or fail
	"""
	cur_ver = str(cur_ver)
	ref_ver = str(ref_ver)
	cur_Varr = [int(vvv) for vvv in cur_ver.split('.')[0:2]]
	cur_V = cur_Varr[0]*10**2 + cur_Varr[1]%100
	ref_Varr = [int(vvv) for vvv in ref_ver.split('.')[0:2]]
	ref_V = ref_Varr[0]*10**2 + ref_Varr[1]%100
	if cur_V > ref_V: return True
	return False

#Run dependency check 
Example 8
Project: compare-codecs   Author: google   File: visual_metrics.py    License: Apache License 2.0 6 votes vote down vote up
def ParseMetricFile(file_name, metric_column):
  """
  Convert a metrics file into a set of numbers.
  This returns a sorted list of tuples with the first number
  being from the first column (bitrate) and the second being from
  metric_column (counting from 0).
  """
  metric_set1 = set([])
  metric_file = open(file_name, "r")
  for line in metric_file:
    metrics = string.split(line)
    if HasMetrics(line):
      if metric_column < len(metrics):
        my_tuple = float(metrics[0]), float(metrics[metric_column])
      else:
        my_tuple = float(metrics[0]), 0
      metric_set1.add(my_tuple)
  metric_set1_sorted = sorted(metric_set1)
  return metric_set1_sorted 
Example 9
Project: Python-John-Zelle-book   Author: sai29   File: 1.py    License: MIT License 6 votes vote down vote up
def main():
    print "This program converts a sequence of ASCII numbers into"
    print "the string of text that it represents."
    print
    
    inString = raw_input("Please enter the ASCII-encoded message: ")

    message = []
    for numStr in string.split(inString):
        asciiNum = eval(numStr) 
        message1 = ord(asciiNum)          # convert digits to a number
        message.append(message1)

    print message
        
    print "The decoded message is:","".join(message) 
Example 10
Project: magpy   Author: geomagpy   File: sigfig.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def round_sig_error(x, ex, n, paren=False):
   '''Find ex rounded to n sig-figs and make the floating point x
   match the number of decimals.  If [paren], the string is
   returned as quantity(error) format'''
   stex = round_sig(ex,n)
   if stex.find('.') < 0:
      extra_zeros = len(stex) - n
      sigfigs = len(str(int(x))) - extra_zeros
      stx = round_sig(x,sigfigs)
   else:
      num_after_dec = len(string.split(stex,'.')[1])
      stx = ("%%.%df" % num_after_dec) % (x)
   if paren:
      if stex.find('.') >= 0:
         stex = stex[stex.find('.')+1:]
      return "%s(%s)" % (stx,stex)
   return stx,stex 
Example 11
Project: magpy   Author: geomagpy   File: sigfig.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def round_sig_error2(x, ex1, ex2, n):
   '''Find min(ex1,ex2) rounded to n sig-figs and make the floating point x
   and max(ex,ex2) match the number of decimals.'''
   minerr = min(ex1,ex2)
   minstex = round_sig(minerr,n)
   if minstex.find('.') < 0:
      extra_zeros = len(minstex) - n
      sigfigs = len(str(int(x))) - extra_zeros
      stx = round_sig(x,sigfigs)
      maxstex = round_sig(max(ex1,ex2),sigfigs)
   else:
      num_after_dec = len(string.split(minstex,'.')[1])
      stx = ("%%.%df" % num_after_dec) % (x)
      maxstex = ("%%.%df" % num_after_dec) % (max(ex1,ex2))
   if ex1 < ex2:
      return stx,minstex,maxstex
   else:
      return stx,maxstex,minstex 
Example 12
Project: CVE-2017-7494   Author: joxeankoret   File: nmb.py    License: GNU General Public License v3.0 6 votes vote down vote up
def encode_name(name, type, scope):
    if name == '*':
        name += '\0' * 15
    elif len(name) > 15:
        name = name[:15] + chr(type)
    else:
        name = string.ljust(name, 15) + chr(type)

    encoded_name = chr(len(name) * 2) + re.sub('.', _do_first_level_encoding, name)
    if scope:
        encoded_scope = ''
        for s in string.split(scope, '.'):
            encoded_scope = encoded_scope + chr(len(s)) + s
        return encoded_name + encoded_scope + '\0'
    else:
        return encoded_name.encode('ascii') + '\0'

# Internal method for use in encode_name() 
Example 13
Project: salt-osx   Author: mosen   File: mac_shadow.py    License: MIT License 6 votes vote down vote up
def _extract_authdata(item):
    '''
    Extract version, authority tag, and authority data from a single array item of AuthenticationAuthority

    item
        The NSString instance representing the authority string

    returns
        version (default 1.0.0), tag, data as a tuple
    '''
    parts = string.split(item, ';', 2)

    if not parts[0]:
        parts[0] = '1.0.0'

    return {
        'version': parts[0],
        'tag': parts[1],
        'data': parts[2]
    } 
Example 14
Project: meddle   Author: glmcdona   File: msvccompiler.py    License: MIT License 6 votes vote down vote up
def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """

    prefix = "MSC v."
    i = string.find(sys.version, prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None 
Example 15
Project: meddle   Author: glmcdona   File: msvccompiler.py    License: MIT License 6 votes vote down vote up
def find_exe(self, exe):
        """Return path to an MSVC executable program.

        Tries to find the program in several places: first, one of the
        MSVC program search paths from the registry; next, the directories
        in the PATH environment variable.  If any of those work, return an
        absolute path that is known to exist.  If none of them work, just
        return the original program name, 'exe'.
        """

        for p in self.__paths:
            fn = os.path.join(os.path.abspath(p), exe)
            if os.path.isfile(fn):
                return fn

        # didn't find it; try existing path
        for p in string.split(os.environ['Path'],';'):
            fn = os.path.join(os.path.abspath(p),exe)
            if os.path.isfile(fn):
                return fn

        return exe 
Example 16
Project: meddle   Author: glmcdona   File: bdist_rpm.py    License: MIT License 6 votes vote down vote up
def _format_changelog(self, changelog):
        """Format the changelog correctly and convert it to a list of strings
        """
        if not changelog:
            return changelog
        new_changelog = []
        for line in string.split(string.strip(changelog), '\n'):
            line = string.strip(line)
            if line[0] == '*':
                new_changelog.extend(['', line])
            elif line[0] == '-':
                new_changelog.append(line)
            else:
                new_changelog.append('  ' + line)

        # strip trailing newline inserted by first changelog entry
        if not new_changelog[0]:
            del new_changelog[0]

        return new_changelog

    # _format_changelog()

# class bdist_rpm 
Example 17
Project: meddle   Author: glmcdona   File: pydoc.py    License: MIT License 6 votes vote down vote up
def source_synopsis(file):
    line = file.readline()
    while line[:1] == '#' or not strip(line):
        line = file.readline()
        if not line: break
    line = strip(line)
    if line[:4] == 'r"""': line = line[1:]
    if line[:3] == '"""':
        line = line[3:]
        if line[-1:] == '\\': line = line[:-1]
        while not strip(line):
            line = file.readline()
            if not line: break
        result = strip(split(line, '"""')[0])
    else: result = None
    return result 
Example 18
Project: meddle   Author: glmcdona   File: pydoc.py    License: MIT License 6 votes vote down vote up
def locate(path, forceload=0):
    """Locate an object by name or dotted path, importing as necessary."""
    parts = [part for part in split(path, '.') if part]
    module, n = None, 0
    while n < len(parts):
        nextmodule = safeimport(join(parts[:n+1], '.'), forceload)
        if nextmodule: module, n = nextmodule, n + 1
        else: break
    if module:
        object = module
        for part in parts[n:]:
            try: object = getattr(object, part)
            except AttributeError: return None
        return object
    else:
        if hasattr(__builtin__, path):
            return getattr(__builtin__, path)

# --------------------------------------- interactive interpreter interface 
Example 19
Project: meddle   Author: glmcdona   File: pydoc.py    License: MIT License 6 votes vote down vote up
def help(self, request):
        if type(request) is type(''):
            request = request.strip()
            if request == 'help': self.intro()
            elif request == 'keywords': self.listkeywords()
            elif request == 'symbols': self.listsymbols()
            elif request == 'topics': self.listtopics()
            elif request == 'modules': self.listmodules()
            elif request[:8] == 'modules ':
                self.listmodules(split(request)[1])
            elif request in self.symbols: self.showsymbol(request)
            elif request in self.keywords: self.showtopic(request)
            elif request in self.topics: self.showtopic(request)
            elif request: doc(request, 'Help on %s:')
        elif isinstance(request, Helper): self()
        else: doc(request, 'Help on %s:')
        self.output.write('\n') 
Example 20
Project: meddle   Author: glmcdona   File: platform.py    License: MIT License 6 votes vote down vote up
def _norm_version(version, build=''):

    """ Normalize the version and build strings and return a single
        version string using the format major.minor.build (or patchlevel).
    """
    l = string.split(version,'.')
    if build:
        l.append(build)
    try:
        ints = map(int,l)
    except ValueError:
        strings = l
    else:
        strings = map(str,ints)
    version = string.join(strings[:3],'.')
    return version 
Example 21
Project: ironpython2   Author: IronLanguages   File: vssutil.py    License: Apache License 2.0 6 votes vote down vote up
def SubstituteInString(inString, evalEnv):
	substChar = "$"
	fields = string.split(inString, substChar)
	newFields = []
	for i in range(len(fields)):
		didSubst = 0
		strVal = fields[i]
		if i%2!=0:
			try:
				strVal = eval(strVal,evalEnv[0], evalEnv[1])
				newFields.append(strVal)
				didSubst = 1
			except:
				traceback.print_exc()
				print "Could not substitute", strVal
		if not didSubst:
			newFields.append(strVal)
	return string.join(map(str, newFields), "") 
Example 22
Project: ironpython2   Author: IronLanguages   File: msvccompiler.py    License: Apache License 2.0 6 votes vote down vote up
def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """

    prefix = "MSC v."
    i = string.find(sys.version, prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None 
Example 23
Project: ironpython2   Author: IronLanguages   File: bdist_rpm.py    License: Apache License 2.0 6 votes vote down vote up
def _format_changelog(self, changelog):
        """Format the changelog correctly and convert it to a list of strings
        """
        if not changelog:
            return changelog
        new_changelog = []
        for line in string.split(string.strip(changelog), '\n'):
            line = string.strip(line)
            if line[0] == '*':
                new_changelog.extend(['', line])
            elif line[0] == '-':
                new_changelog.append(line)
            else:
                new_changelog.append('  ' + line)

        # strip trailing newline inserted by first changelog entry
        if not new_changelog[0]:
            del new_changelog[0]

        return new_changelog

    # _format_changelog()

# class bdist_rpm 
Example 24
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, line):
        """Initialize the word from a line of a WN POS file."""
	tokens = string.split(line)
	ints = map(int, tokens[int(tokens[3]) + 4:])
	self.form = string.replace(tokens[0], '_', ' ')
        "Orthographic representation of the word."
	self.pos = _normalizePOS(tokens[1])
        "Part of speech.  One of NOUN, VERB, ADJECTIVE, ADVERB."
	self.taggedSenseCount = ints[1]
        "Number of senses that are tagged."
	self._synsetOffsets = ints[2:ints[0]+2] 
Example 25
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, pos, offset, line):
        "Initialize the synset from a line off a WN synset file."
	self.pos = pos
        "part of speech -- one of NOUN, VERB, ADJECTIVE, ADVERB."
	self.offset = offset
        """integer offset into the part-of-speech file.  Together
        with pos, this can be used as a unique id."""
	tokens = string.split(line[:string.index(line, '|')])
	self.ssType = tokens[2]
	self.gloss = string.strip(line[string.index(line, '|') + 1:])
        self.lexname = Lexname.lexnames[int(tokens[1])]
	(self._senseTuples, remainder) = _partition(tokens[4:], 2, string.atoi(tokens[3], 16))
	(self._pointerTuples, remainder) = _partition(remainder[1:], 4, int(remainder[0]))
	if pos == VERB:
	    (vfTuples, remainder) = _partition(remainder[1:], 3, int(remainder[0]))
	    def extractVerbFrames(index, vfTuples):
		return tuple(map(lambda t:string.atoi(t[1]), filter(lambda t,i=index:string.atoi(t[2],16) in (0, i), vfTuples)))
	    senseVerbFrames = []
	    for index in range(1, len(self._senseTuples) + 1):
		senseVerbFrames.append(extractVerbFrames(index, vfTuples))
	    self._senseVerbFrames = senseVerbFrames
	    self.verbFrames = tuple(extractVerbFrames(None, vfTuples))
            """A sequence of integers that index into
            VERB_FRAME_STRINGS. These list the verb frames that any
            Sense in this synset participates in.  (See also
            Sense.verbFrames.) Defined only for verbs.""" 
Example 26
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 5 votes vote down vote up
def setupLexnames():
    for l in open(WNSEARCHDIR+'/lexnames').readlines():
        i,name,category = string.split(l)
        Lexname(name,PartsOfSpeech[int(category)-1]) 
Example 27
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 5 votes vote down vote up
def keys(self):
	if hasattr(self, 'indexCache'):
	    keys = self.indexCache.keys()
	    keys.sort()
	    return keys
	else:
	    keys = []
	    self.rewind()
	    while 1:
		line = self.file.readline()
		if not line: break
                key = line.split(' ', 1)[0]
		keys.append(key.replace('_', ' '))
	    return keys 
Example 28
Project: razzy-spinner   Author: rafasashi   File: toolbox.py    License: GNU General Public License v3.0 5 votes vote down vote up
def raw(files='rotokas.dic', include_header=False, head_field_marker=None):
    """
    Deprecated: use C{StandardFormat.fields()}
    
    @param files: One or more toolbox files to be processed
    @type files: L{string} or L{tuple(string)}
    @param include_header: flag that determines whether to treat header as record (default is no)
    @type include_header: boolean
    @param head_field_marker: option for explicitly setting which marker to use as the head field
                              when parsing the file (default is automatically determining it from
                              the first field of the first record)
    @type head_field_marker: string
    @rtype: iterator over L{list(string)}
    """       

    # Just one file to process?  If so convert to a tuple so we can iterate
    if type(files) is str : files = (files,)

    for file in files:
        path = os.path.join(get_basedir(), "toolbox", file)
        fc = open(path, "U").read()
        if fc.strip().startswith(r"\_") :
            (header, body) = split(fc, sep="\n\n", maxsplit=1)
            if include_header:
                yield list(_parse_record(header))
        else :
            body = fc
            
        # Deal with head field marker
        if head_field_marker :
            hfm_with_backslash = "\\" + hfm
        else :
            ff = split(body, sep="\n", maxsplit=1)[0]                # first field
            hfm_with_backslash = split(ff, sep=" ", maxsplit=1)[0]   # raw marker of first field
        recordsep = "\n\n"+hfm_with_backslash                        # separates records from one another
        
        # Parse records
        for r in split("\n\n"+body, sep=recordsep)[1:] : 
            yield list(_parse_record(hfm_with_backslash + r))

# assumes headwords are unique 
Example 29
Project: razzy-spinner   Author: rafasashi   File: ElementTree.py    License: GNU General Public License v3.0 5 votes vote down vote up
def fixtag(tag, namespaces):
    # given a decorated tag (of the form {uri}tag), return prefixed
    # tag and namespace declaration, if any
    if isinstance(tag, QName):
        tag = tag.text
    namespace_uri, tag = string.split(tag[1:], "}", 1)
    prefix = namespaces.get(namespace_uri)
    if prefix is None:
        prefix = _namespace_map.get(namespace_uri)
        if prefix is None:
            prefix = "ns%d" % len(namespaces)
        namespaces[namespace_uri] = prefix
        if prefix == "xml":
            xmlns = None
        else:
            xmlns = ("xmlns:%s" % prefix, namespace_uri)
    else:
        xmlns = None
    return "%s:%s" % (prefix, tag), xmlns

##
# Parses an XML document into an element tree.
#
# @param source A filename or file object containing XML data.
# @param parser An optional parser instance.  If not given, the
#     standard {@link XMLTreeBuilder} parser is used.
# @return An ElementTree instance 
Example 30
Project: razzy-spinner   Author: rafasashi   File: wordnet.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, line):
        """Initialize the word from a line of a WN POS file."""
	tokens = string.split(line)
	ints = map(int, tokens[int(tokens[3]) + 4:])
	self.form = string.replace(tokens[0], '_', ' ')
        "Orthographic representation of the word."
	self.pos = _normalizePOS(tokens[1])
        "Part of speech.  One of NOUN, VERB, ADJECTIVE, ADVERB."
	self.taggedSenseCount = ints[1]
        "Number of senses that are tagged."
	self._synsetOffsets = ints[2:ints[0]+2]