f=open('insert.dat','w+')
while True:
name=raw_input("ENter name:")
age=input("Enter age")
gen=raw_input("gender")
f.write(name+','+str(age)+','+gen+'')
ch=raw_input("continue")
if(ch=='n'):
break
f=open('insert.dat','r+')
x=f.readline()
x=x.split(',')
for index,line in enumerate(x):
print line,index
f.seek(0,0)
f.close()
in this program, i want to input:
name:lol
age:3
gender:F
name:koi
age:4
gender:F
so x.split should come as
['lol',3,'F','koi',4,'F']
now i want to get first details as a separate list , like:
['lol',3,'F'].
but when i use the above format, each word is coming as a list when used along with split. how to get like this using enumerate and split only.?? Thankyou!
Using the same logic used by you. The thing you are doing wrong is adding the next details to the end of the previous data.
You can do the below to avoid it:
f=open('insert.dat','w+')
individual = []
while True:
name=raw_input("ENter name:")
age=input("Enter age")
gen=raw_input("gender")
f.write(','+name+','+str(age)+','+gen+'')
individual.append([name,age,gen])
ch=raw_input("continue")
if(ch=='n'):
break
f=open('insert.dat','r+')
x=f.readline()
x=x.replace(',', '', 1)
x=x.split(',')
for index,line in enumerate(x):
print line,index
f.seek(0,0)
f.close()
for ind in individual:
print ind
I have added comma ',' at the beginning of the input. All you need to do is replace the first comma before splitting the string.