Python - Manipulating data retrived from a text file -


i have write function retrieves data text file , read numbers. figured need loop this, far have :

f= "textfile.txt" while f.readline() != "":  #while line not empty do: 

the data in text file example:

3,2,4,1 1,,4,2 13,2,16,3 etc... 

so have read data , error detection if data isn't in correct ones above. there dont need 3 numbers empty spots in "1,,4" replaced 0 , "1,0,4"

data correction this:

14,3,,2      become  14,3,0,2 ,,4,        become  0,0,4,0 1,2,3,4,5   become [] (empty there many numbers) ,,,,        become [] (empty there no numbers) 0,0,0,0     become []  (empty there is'nt @ least 1 number > 0 ) 3,2,-7,8    become [] (empty there negative number) 3,2,7.3,8   become [] (empty there float)       

so basically, there blank spaces or positive intergers allowed.

my basic understanding of need use split function numbers individualy , error detection. have function basic error detection individual numbers

def detect(s) if s == "" :    return 0 if s < 0 :    return -1 if s > 0 :    return s 

help appreciated. in advance

you can use list comprehension on splitted data:

data='1,,4,2' result=[int(d) if d else 0 d in data.split(',')] print result 

output:

[1, 0, 4, 2] 

you can on each line.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -