split - Python: (IndexError: list index out of range) readFile -


i got text in text file file:

_2015.9.30 - 15:36:3 , 13_ _2015.9.30 - 15:36:6 , 24_ _2015.9.30 - 15:36:8 , 33_ 

and want have this

_data=['2015.9.30 - 15:36:3', '2015.9.30 - 15:36:6', '2015.9.30 -15:36:8']_ _values=['13', '24', '33']_ 

so tried code

def getdata(path):    data = []     readfile = open(path,'r')    sepfile = readfile.read().split('\n')    readfile.close()     in sepfile:        mydata = i.split(',')        data.append(mydata[0])     return data    def getvalues (path):    values = []     readfile = open(path,'r')    sepfile = readfile.read().split('\n')    readfile.close()     in sepfile:        myvalues = i.split(',')        values.append(myvalues[1])     return values  print getdata("mytext.txt") print getvalues("mytext.txt") 

the first method getdata works fine second 1 dont want work .. errormessage:

['2015.9.30 - 15:36:3 ', '2015.9.30 - 15:36:6 ', '2015.9.30 - 15:36:8']   traceback (most recent call last): file "c:\python27\z.new\schrottplatz.py", line 34, in <module> print getvalues("mytext.txt") file "c:\python27\z.new\schrottplatz.py", line 29, in getvalues values.append(myvalues[1]) indexerror: list index out of range 

file.txt

_2015.9.30 - 15:36:3 , 13_ _2015.9.30 - 15:36:6 , 24_ _2015.9.30 - 15:36:8 , 33_ 

code

with open('file.txt') f:     data = f.read().splitlines()  _data, _values = [], [] d in data:     val = d.split(' , ')     _data.append(val[0][1:])     _values.append(val[1][:-1])  print _data print _values #['2015.9.30 - 15:36:3', '2015.9.30 - 15:36:6', '2015.9.30 - 15:36:8'] #['13', '24', '33'] 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -