Guest User

Untitled

a guest
Feb 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. altura = np.loadtxt("bio.dat",delimiter=',',usecols=(5,),dtype='float')
  2. File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 846, in loadtxt
  3. vals = [vals[i] for i in usecols]
  4. IndexError: list index out of range
  5.  
  6. import numpy as np
  7.  
  8. altura = np.loadtxt("bio.dat",delimiter=',',usecols=(5,),dtype='str')
  9. print altura
  10.  
  11. 1 Katherine Oquendo M 18 1.50 50
  12. 2 Pablo Restrepo H 20 1.83 79
  13. 3 Ana Agudelo M 18 1.58 45
  14. 4 Kevin Vargas H 20 1.74 80
  15. 5 Pablo madrid H 20 1.70 55
  16.  
  17. x=sum(altura)
  18.  
  19. import csv
  20.  
  21. txt_file = r"mytxt.txt"
  22. csv_file = r"mycsv.csv"
  23.  
  24. # use 'with' if the program isn't going to immediately terminate
  25. # so you don't leave files open
  26. # the 'b' is necessary on Windows
  27. # it prevents x1a, Ctrl-z, from ending the stream prematurely
  28. # and also stops Python converting to / from different line terminators
  29. # On other platforms, it has no effect
  30. in_txt = csv.reader(open(txt_file, "rb"), delimiter = 't')
  31. out_csv = csv.writer(open(csv_file, 'wb'))
  32.  
  33. out_csv.writerows(in_txt)
  34.  
  35. In [17]: from StringIO import StringIO
  36. In [18]: s="""
  37. 1 Katherine Oquendo M 18 1.50 50
  38. 2 Pablo Restrepo H 20 1.83 79
  39. 3 Ana Agudelo M 18 1.58 45
  40. 4 Kevin Vargas H 20 1.74 80
  41. 5 Pablo madrid H 20 1.70 55
  42. """
  43. In [19]: S=StringIO(s)
  44. In [20]: data=np.loadtxt(S,dtype=float,usecols=(5,))
  45. In [21]: data
  46. Out[21]: array([ 1.5 , 1.83, 1.58, 1.74, 1.7 ])
  47. In [22]: np.sum(data)
  48. Out[22]: 8.3499999999999996
  49.  
  50. import numpy as np
  51. fname = 'stack25828405.txt'
  52. data=np.loadtxt(fname,dtype=float,usecols=(5,))
  53. print data
  54. print np.sum(data)
  55.  
  56. 2119:~/mypy$ python2.7 stack25828405.py
  57. [ 1.5 1.83 1.58 1.74 1.7 ]
  58. 8.35
  59.  
  60. altura = np.loadtxt("bio.dat",delimiter=',',usecols=(5,),dtype=β€˜str’,comments='')
Add Comment
Please, Sign In to add comment