How to read input file for correct int list? python, list, -


my input list of int elements save list file (not more, not less), works fine far. when comes taking file input int list, either wrong list elements or error code. numbers can (positive) int , it's number.

input file (by keyboard):

1, 2, 3, 4 

file content:

[1, 2, 3, 4] 

list after input file:

['[1', '2', '3', '4]'] 

where should be:

[1, 2, 3, 4] 

the elements of filst file need int again.

l = list_from_file = 0 l [ 1 ] = 2  def take ( profil , s , succ) :     = 0     j = 0     n = 0     erf = false     if s :         print ( "\nput list in." )# supposed list         profil = [ int ( x ) x in input ( ).split ( sep = ', ' ) ]         s = false     else :         profil = input ( "\nput list in. again." ).split ( sep = ', ' )     erf = check( profil )     if not erf :         erf = ask ( profil , s , succ)     return profil , s  def check( profil ) :     = 0     b = true     n = 0     n in profil [ : ] : #        if  int ( profil [ n ] ) < 0 : #some confusing errors tried fix these possibilities... #        if  profil [ n ] < 0 :         if  int ( n ) < 0 : #        if  n < 0 :             b = false             exit         += 1     -= 1     if ( profil [ -1 ] != profil [ ] ) :          b = false     return b def ask( profil , s , succ) :     show( profil , succ)     s = check( profil )     if s :         profil = input ( "\nput list in." ).split ( sep = ', ' )                 s = false     else :         profil = input ( "\nput list in. again." ).split ( sep = ', ' ) #    if profil [ 0 ] != 0 : #        s = ask     return succ, s  def save( profil , path) :     path = input ( "put path in: " )     erf = false     if os.path.isfile ( path) :          inp= input ( "file exists. overwrite[u], bring input [e] or ignore[i]?" )         if inp== 'u' or inp== 'u' :             f = open ( path, "w" )             f.write ( str ( profil ) )         elif inp== 'e' or inp== 'e' :             f = open ( path, "r" )                                                               profil = f.read ( ).split ( sep = ', ' )          elif inp== 'i' or inp== 'i' :             f = open ( path, "r" )                                                          print ( "file closed." )             f = f.close         else :             inp= input ( "confusing input. continue key." )             return profil     else :         print ( "file made." )         f = open ( path, "w" )         f.write ( profil )         f = f.close     return profil def bring( path, profil ) :     path= input ( "\npath: " )         f = open ( path, "r" )     profil = f.read ().split ( sep = ', ' ) #    profil = [ int ( x ) x in input ( ).split ( sep = ', ' ) ] #    profil = profil ().read().replace ( '[' , '' ) #    profil = f.read [ : ].replace ( ']' , '' )#also variants tried.     f = f.close #    profil = strrep ( profil )#new function tried     print (profil)     return profil   def delete ( path, succ) :     erf = false     path= input ( "put in deleting path." )     if os.path.isfile ( path) :         os.remove ( path)                 print ( "file " + path+ " deleted." )         erf = true     else :         print ( "datei nicht gefunden." )     return erf  inp = input("please start.") while ( inp != 'q' ) , ( inp != 'q' ) :     elif inp == 'n' :         inp = input ( "and now?" )     elif inp == 'p' or inp == 'p' :           profil , s = take ( profil , s , succ )         succ = zeigen ( profil , succ )         if profil [ 0 ] == '0' :             print ( "profil not usable.." )         else :             inp = input ( "and now?" )     elif inp == 'z' or inp == 'z' :         succ = show ( profil , succ )         inp = 'n'     elif inp == 's' or inp == 's' :         profil = save ( profil , path )         inp = 'n'     elif inp == 'e' or inp == 'e' :         profil = bring( path , profil )         print(profil )         dif = profil [ 0 ]         inp = 'n'     elif inp == 'l' or inp == 'l' :         succ = delete ( path , succ )         inp = 'n'     else :         inp = input ( "unknown command. quit q..." ) if ( inp == 'q' ) or ( inp == 'q' ) :     quit ( ) 

you have several options here.

  • save each number in own line instead of actual list, in order read file list of ints:

    with open(filename) f:     list_of_ints = [int(line.strip()) line in f] 
  • if insist on writing list as-is file, can use literal_eval (do not use eval):

    from ast import literal_eval  open(filename) f:     list_of_ints = literal_eval(f.read().strip()) 

keep in mind usage of strip() rid of possible leading/trailing spaces and/or new line characters.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -