Guest User

Untitled

a guest
May 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. ///
  2. * ABC Names
  3. A-06,B-18,
  4. * Data
  5. 1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01,
  6. 1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02,
  7. 2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02,
  8. 1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01,
  9. 9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02,
  10. * Starting position
  11. -.5000E+01
  12. ///
  13.  
  14. f = open("mdata.txt")
  15.  
  16. data_dict = {}
  17. section = None
  18. data_for_section = ""
  19. for line in f:
  20. line = line.strip() #remove whitespace at start and end
  21.  
  22. if section != None and (line[0] == "*" or line == "///"):
  23. # if we've just finished a section, put whatever we got into the data dict
  24. data_dict[section] = [bit for bit in data_for_section.split(",") if bit != ""]
  25.  
  26. if line[0] == "*":
  27. # "*" denotes the start of a new section, probably, so remember the name
  28. section = line [2:]
  29. data_for_section = ""
  30. continue
  31. data_for_section += line
  32.  
  33. f.close()
  34. #got the data, now for some output
  35. print "loaded file. Found headings: %s"%(", ".join(data_dict.keys()))
  36.  
  37. for key in data_dict.keys():
  38. if len(data_dict[key])>5:
  39. print key, ": array of %i entries"%len(data_dict[key])
  40. else:
  41. print key, ": ", data_dict[key]
  42.  
  43. startingPosition = float(data_dict["Starting position"][0])
  44. data_list_of_floats = map(float, data_dict["Data"])
  45.  
  46. startingPosition = float(data_dict["Starting position"][0])
  47. data_list_of_floats = map(float, data_dict["Data"])
  48.  
  49. f = open("abc.txt")
  50. all_lines = f.readlines()
  51.  
  52. f=0
  53. for line in open("file"):
  54. line=line.strip()
  55. if "Starting" in line:
  56. f=0
  57. if "Data" in line:
  58. f=1
  59. continue
  60. if f:
  61. print line
  62.  
  63. {'a':['line1','line2'],'b':['line1'],'c':['line1','line2','line3']}
  64.  
  65. block_names = ['b','a','c']
  66.  
  67. for line in open('file.txt'):
  68. block_dict = {} #dict to populate with lists of lines
  69. block = [] # dummy block in case there is data or lines before first block
  70. ck_nm = [blk_nm for blk_nm in block_names if line.startswith(blk_nm)] #search block names for a match
  71. if ck_nm: # did we find a match?
  72. block_dict[ck_nm[0]] = block = [] # set current block
  73. else:
  74. block.append(line) #..or line.split(',') ..however you want to parse the data
  75.  
  76. data = [
  77. 1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01,
  78. 1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02,
  79. 2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02,
  80. 1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01,
  81. 9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02,
  82. ]
Add Comment
Please, Sign In to add comment