Python: read text file into list

24 12 2012

20121224

text file contain data of 15 ground point. file name is grounddata.txt

 15
 1 18160.153 -57509.869 1284.700 0.2 0.2 0.1
 2 24207.094 -54489.362 1341.192 0.2 0.2 0.1
 3 27248.245 -51442.223 824.272 0.2 0.2 0.1
 4 21195.711 -51470.131 946.992 0.2 0.2 0.1
 5 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 6 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 7 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 8 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 9 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 10 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 11 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 12 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 13 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 14 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0
 15 24000.0 -54000.0 1000.0 1000.0 1000.0 1000.0

the first line is number of points.
second line to end is point number, X, Y, Z, SDX,SDY,SDZ

I need to read text file and convert data from text to number and add them to list in Python.
I use this code

def num(s):
  try:
     return int(s)
  except ValueError:
     return float(s)
if __name__ == '__main__':
   filemane = 'grounddataTom.txt'
   with open(filemane,'r') as f:
     i = 0
     groundPointList = []
     for line in f:
       if i == 0:
         groundPoint = int(line)
       else:
         lineTxt = line.split()
         lineNum = []
           for j in lineTxt:
             lineNum.append(num(j))
           groundPointList.append(lineNum)
       i +=1
  print groundPoint
  print groundPointList

it work very well


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: