-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_json.py
109 lines (100 loc) · 2.5 KB
/
read_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#this tests reading in numeric data from a file
#May 2013
import numpy , json
file=open('test_data.txt', 'r')
### Reading in a file with tab separated floats
#data=[]
#counter=0
#for line in file:
# if ('#' == line[0]):
# continue #skip comment lines
# else:
# data.append([float(item) for item in line.split()])
#
##print data[:10]
##print numpy.array(data)[:10]
#
#file.close()
#
#file=open('test_data.txt', 'r')
#text = file.read() #text is just a long string with all the contents of the file
#file.close()
##print len(text)
#
#text_array= numpy.array(text)
##print text_array.shape
##print text_array
### getting the funciton data from a json
file=open('test_data.json', 'r')
stringmatrix = '['
line_count=0
function_name="Self_Energy"
for line in file:
line_count+=1
if function_name in line:
for line in file:
line_count+=1
if "VALUES" in line:
start_line=line_count
for line in file:
line_count+=1
if "}" in line:
end_line = line_count
break
else:
stringmatrix = stringmatrix+line
break
break
file.close()
print 'line count: ', line_count
print 'start values:', start_line #where the data starts
print 'end values:', end_line #where the data ends
functiondata = json.loads(stringmatrix)
datamatrix = numpy.matrix(functiondata)
#print len(stringmatrix)
#print stringmatrix
#print functiondata
#print datamatrix
#print datamatrix.shape
### replace part of the contents of the file
file= open('test_data.json', 'r')
data=[]
for line in file:
data.append(line)
file.close()
for i in range(start_line-1, end_line-1):
print data[i]
print data[i].split()
### reading the file as string and finding the start and end of the data as string positions, not lines
#file=open('test_data.json', 'r')
#text = file.read()
#file.close()
#
#i=0
#valend =-1
#while i<len(text):
# if text[i:i+len("Self_Energy")]=="Self_Energy":
# print 'SE', i
# i+=1
# while i < len(text):
# if text[i:i+len("VALUES")]=="VALUES":
# valstart = i+len("VALUES")+7
# print 'begin values', valstart
# print text[valstart]
# i+=1
# while i < len(text):
# if text[i]=="}":
# valend = i-5
# print "end values", valend
# print text[valend]
# break
# else:
# i+=1
# else:
# if valend>-1: break
# else: i+=1
# else:
# if valend>-1: break
# else: i+=1
#print i, valstart, valend
#print len(text)