Using readlines in python? First time

I have a text file with data columns, and I need to turn these columns into separate lists or arrays. This is what I still have

f = open('data.txt', 'r') temp = [] for row in f.readlines(): Data = row.split() temp.append(float(Data[0])) 

When I run this, I get IndexError: list index out of range .

The following is a snippet of data:

 16 0.2000 17 0.3000 18 0.4000 20 0.5000 21 0.6000 22 0.7000 24 0.8000 25 0.9000 26 1.000 

I need the first column, if possible: Data = [16, 17, 18, 20, 21, 22, 24, 25, 26]

+7
source share
7 answers

You get an empty list Data=[] if you read an empty line. You are trying to get the first item from a list using Data[0] , but since it is an empty list, it does not have an item at position 0, so you get an IndexError .

 Data=''.split() Data[0] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-686-0792b03cbbdf> in <module>() ----> 1 Data[0] IndexError: list index out of range 

This will output the text Data if IndexError occours - you will see for yourself that it prints an empty list:

 f=open('file','r') temp = [] for row in f.readlines(): Data = row.split() try: temp.append(float(Data[0])) except IndexError: print Data 

You can use the with statement to open a file that automatically closes the file after processing. You can also scroll the file itself without using readlines() .

 with open(file,'r') as f: for row in f: Data = row.split() try: print Data[0] except IndexError: print 'You have an empty row' 

EDIT: you better use the csv module:

 import csv with open('file.csv', 'rb') as f: reader = csv.reader(f, delimiter=' ') print [row[0] for row in reader if len(row)] >>> ['16', '17', '18', '20', '21', '22', '24', '25', '26'] 
+5
source

use for file manipulators.

 with open('path/to/file', 'r') as f: for line in f: # code. 

an indexing error occurs from an attempt to access Data at location [0] . which just means your line is empty.

you should do a quick check before parsing the string ...

 if len(Data): #code else: #empty line? 
+2
source
 f = open('data.txt', 'r') temp = [] for row in f.readlines(): items = row.split(',') temp.append(unicode(items[0])) 

I hope it solves your problem.

0
source
 def cal(num_list): x = 1; z = 0; while True: list1 = list(str(x)) list2 = [int(a) for a in list1] for i in range(len(list2)): for j in range(10): if(list2.count(j) > num_list[list2[i]]): z = 1; break; if(z == 1): save(x); break; x = x + 1; 
0
source
 #matrix A to B def show(): global string_final,list_2,tot index=matrix_1.index(num) # finding index of num length=n j=(index)%length # the column positon i=index/length # the row position list_1=[] for a in range(length): lis=[] for b in range(length): lis.append(matrix_1.pop(0)) #pop the first element and append the list list_1.append(lis) tot=0 list_2=[] for a in range(i+1): for b in range(j+1): tot=tot+list_1[a][b] # add the numbers list_2.append(list_1[a][b]) #append to list if(b!=length): list_2.append(" ") #append space list_2.append("\n") string_final="".join([str(a) for a in list_2]) #list to string print "matrix B\n",string_final,"\nx: ",str(num),"\nn: ",str(n)+"\ntotal: "+str(tot) # print the result 
0
source
 #matrix add zero def get(): global b_no num_list=[]; file=open("matrix.txt","r"); for i in file: b_no=0 if(len(i.split())==1): #check length is 1 or not n=int(i)#string to int else: temp=i.split(); if(len(temp)!=n): b_no+=1 break temp=[0]+[int(a) for a in temp]+[0] #add zero at first and last num_list.append(temp); zero_list=[] for i in range(n+2): zero_list.append(0) #append zero num_list.insert(0,zero_list) num_list.insert(n+1,zero_list) return num_list,n; 
0
source

I would not use Data [0] or row [0], instead I would use '.join (Data) or'.join (row) to avoid an empty list (index index error out of range).

0
source

All Articles