def load_text_file(i):
    """
    Input:  {
              text_file           - name of text file
              (keep_as_bin)       - if 'yes', return only bin
              (encoding)          - by default 'utf8', however sometimes we use utf16

              (split_to_list)     - if 'yes', split to list

              (convert_to_dict)   - if 'yes', split to list and convert to dict
              (str_split)         - if !='', use as separator of keys/values when converting to dict
              (remove_quotes)     - if 'yes', remove quotes from values when converting to dict

              (delete_after_read) - if 'yes', delete file after read (useful when reading tmp files)
            }

    Output: {
              return       - return code =  0, if successful
                                         = 16, if file not found (may be warning)
                                         >  0, if error
              (error)  - error text if return > 0

              bin      - bin
              (string) - loaded text (with removed \r)
              (lst)    - if split_to_list=='yes', return as list
              (dict)   - if convert_to_dict=='yes', return as dict
            }
    """

    fn=i['text_file']

    en=i.get('encoding','')
    if en=='' or en==None: en='utf8'

    try:
       f=open(fn, 'rb')
    except Exception as e:
       return {'return':16, 'error':'problem opening text file='+fn+' ('+format(e)+')'}

    try:
       b=f.read()
    except Exception as e:
       f.close()
       return {'return':1, 'error':'problem reading text file='+fn+' ('+format(e)+')'}

    f.close()

    r={'return':0, 'bin':b}

    if i.get('delete_after_read','')=='yes':
       import os
       os.remove(fn)

    if i.get('keep_as_bin','')!='yes':
       s=b.decode(en).replace('\r','') # decode into Python string (unicode in Python3)
       r['string']=s

       cl=i.get('split_to_list','')
       cd=i.get('convert_to_dict','')

       if cl=='yes' or cd=='yes':
          lst=s.split('\n')
          r['lst']=lst

          if cd=='yes':
             dd={}

             ss=i.get('str_split','')
             rq=i.get('remove_quotes','')
             if ss=='': ss=':'

             for q in lst:
                 qq=q.strip()
                 ix=qq.find(ss)
                 if ix>0:
                    k=qq[0:ix].strip()
                    v=''
                    if ix+1<len(qq):
                       v=qq[ix+1:].strip()
                    if v!='' and rq=='yes':
                       if v.startswith('"'): v=v[1:]
                       if v.endswith('"'): v=v[:-1]
                    dd[k]=v

             r['dict']=dd

    return r
def load_text_file ( i ) : fn = i [ 'text_file' ] en = i . get ( 'encoding' , '' ) if en == '' or en == None : en = 'utf8' try : f = open ( fn , 'rb' ) except Exception as e : return { 'return' : 16 , 'error' : 'problem opening text file=' + fn + ' (' + format ( e ) + ')' } try : b = f . read ( ) except Exception as e : f . close ( ) return { 'return' : 1 , 'error' : 'problem reading text file=' + fn + ' (' + format ( e ) + ')' } f . close ( ) r = { 'return' : 0 , 'bin' : b } if i . get ( 'delete_after_read' , '' ) == 'yes' : import os os . remove ( fn ) if i . get ( 'keep_as_bin' , '' ) != 'yes' : s = b . decode ( en ) . replace ( '\r' , '' ) r [ 'string' ] = s cl = i . get ( 'split_to_list' , '' ) cd = i . get ( 'convert_to_dict' , '' ) if cl == 'yes' or cd == 'yes' : lst = s . split ( '\n' ) r [ 'lst' ] = lst if cd == 'yes' : dd = { } ss = i . get ( 'str_split' , '' ) rq = i . get ( 'remove_quotes' , '' ) if ss == '' : ss = ':' for q in lst : qq = q . strip ( ) ix = qq . find ( ss ) if ix > 0 : k = qq [ 0 : ix ] . strip ( ) v = '' if ix + 1 < len ( qq ) : v = qq [ ix + 1 : ] . strip ( ) if v != '' and rq == 'yes' : if v . startswith ( '"' ) : v = v [ 1 : ] if v . endswith ( '"' ) : v = v [ : - 1 ] dd [ k ] = v r [ 'dict' ] = dd return r
Input:  {
              text_file           - name of text file
              (keep_as_bin)       - if 'yes', return only bin
              (encoding)          - by default 'utf8', however sometimes we use utf16

              (split_to_list)     - if 'yes', split to list

              (convert_to_dict)   - if 'yes', split to list and convert to dict
              (str_split)         - if !='', use as separator of keys/values when converting to dict
              (remove_quotes)     - if 'yes', remove quotes from values when converting to dict

              (delete_after_read) - if 'yes', delete file after read (useful when reading tmp files)
            }

    Output: {
              return       - return code =  0, if successful
                                         = 16, if file not found (may be warning)
                                         >  0, if error
              (error)  - error text if return > 0

              bin      - bin
              (string) - loaded text (with removed \r)
              (lst)    - if split_to_list=='yes', return as list
              (dict)   - if convert_to_dict=='yes', return as dict
            }