User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
other:python:misc_by_jyp [2022/02/21 17:46] – [numpy related stuff] Added reduceat jypeterother:python:misc_by_jyp [2022/03/08 17:40] – [Playing with strings] jypeter
Line 39: Line 39:
 True</code> True</code>
  
 +==== Playing with strings ====
 +
 +=== Filenames, etc... ===
 +
 +Check [[other:python:misc_by_jyp#working_with_paths_and_filenames|Working with paths and filenames]] and [[other:python:misc_by_jyp#generating_file_names|Generating file names]]
 +
 +=== Splitting strings ===
 +
 +It's easy to split a string with multiple blank delimiters, or a specific delimiter, but it can be harder to deal with sub-strings
 +
 +<code>>>> str_with_blanks = 'one    two\t3\t\tFOUR'
 +>>> str_with_blanks.split()
 +['one', 'two', '3', 'FOUR']
 +
 +>>> str_with_simple_delimiters = '1,2,3.14,  4'
 +>>> str_with_simple_delimiters.split(',')
 +['1', '2', '3.14', 4']
 +
 +>>> complex_string='-o 1 --long "A string with accented chars: é è à ç"'
 +>>> complex_string.split()
 +['-o', '1', '--long', '"A', 'string', 'with', 'accented', 'chars:', '\xc3\xa9', '\xc3\xa8', '\xc3\xa0', '\xc3\xa7"']
 +
 +>>> import shlex
 +>>> shlex.split(complex_string)
 +['-o', '1', '--long', 'A string with accented chars: \xc3\xa9 \xc3\xa8 \xc3\xa0 \xc3\xa7']</code>
 ==== Working with paths and filenames ==== ==== Working with paths and filenames ====
  
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki