writing on file not until the end

A

Alexzive

Hello,
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

Many thanks for your help!
Alex

the original file to crop (in path) looks like this:

*HEADING
ABAQUS-style file created by OOF2 on 2009-03-05 03:23:49.204607 from a
mesh of the microstructure A.
** Materials defined by OOF2:
** C:
** W:
** Master elements used in OOF2:
** 2: D2_2, Isoparametric 2-noded edge element
** 3: T3_3, Isoparametric 3 noded triangle with linear interpolation
for both fields and positions.
** 4: Q4_4, Isoparametric 4 noded quadrilateral with bilinear
interpolation for both positions and fields
** Boundary Conditions:
** Notes:
** The set of nodes and elements may be different from the set
created from a skeleton
** depending on the element type and if the mesh was refined.
** The materials and boundary conditions provided by OOF2 may be
translated into ABAQUS by the user.
** The element type provided below should be verified and modified
accordingly.
** Only elements (and nodes of such elements) that have an
associated material are included in this file.
*NODE
6, 0.0, 0.0
1, 0.031365, 0.0
9, 0.06273, 0.0
10, 0.094095, 0.0
17, 0.12546, 0.0
18, 0.156825, 0.0
29, 0.18819, 0.0
33, 0.219555, 0.0
37, 0.25092, 0.0
38, 0.282285, 0.0
245, 0.31365, 0.0
4, 0.0, 0.027505735912
7, 0.0283153247289, 0.0247564240049
8, 0.06273, 0.0236123
13, 0.126933916405, 0.0219324392255
22, 0.156825, 0.0236123
25, 0.185442602873, 0.0174188364874
35, 0.216158167035, 0.0260087067213
41, 0.25092, 0.0236123
65, 0.31365, 0.0265806479154
45, 0.0, 0.0472246
44, 0.0270022417013, 0.0457250093507
46, 0.06273, 0.0472246
47, 0.0987777090467, 0.0476663143118
48, 0.123432920369, 0.0493041533953
50, 0.157517619392, 0.0491485665468
54, 0.18819, 0.0472246
56, 0.21829479153, 0.0489914989989
59, 0.255396164365, 0.053642386456
62, 0.283534894472, 0.0482125495477
66, 0.31365, 0.0473757108379
68, 0.0, 0.0708369
71, 0.031365, 0.0708369
73, 0.0667269869261, 0.0724057787737
74, 0.094095, 0.0708369
75, 0.122035555525, 0.0683265441516
77, 0.154069643632, 0.067833220225
79, 0.192676294482, 0.0697610582702
80, 0.218433034079, 0.0733140035725
81, 0.250592391777, 0.0705659765909
115, 0.31365, 0.0749934030765
86, 0.0, 0.0937712452856
87, 0.0327416679167, 0.0988320724937
92, 0.0642181016634, 0.0996865210439
94, 0.0939583966251, 0.0960413983543
98, 0.12277254344, 0.0956157005759
101, 0.156825, 0.0944492
107, 0.190080813107, 0.0989004943145
109, 0.219555, 0.0944492
111, 0.25092, 0.0944492
116, 0.31365, 0.0913902112346
117, 0.0, 0.118185969919
118, 0.031365, 0.1180615
119, 0.0657061477957, 0.117247512024
120, 0.0954076666726, 0.117579706786
122, 0.120457073874, 0.118852698163
125, 0.150412085671, 0.119805228459
128, 0.188401966746, 0.118578758447
132, 0.219555, 0.1180615
131, 0.25092, 0.1180615
134, 0.282285, 0.1180615
138, 0.0, 0.1416738
137, 0.031365, 0.1416738
139, 0.06273, 0.1416738
140, 0.0981318888408, 0.141731507791
160, 0.156825, 0.1416738
145, 0.18819, 0.1416738
144, 0.21849787291, 0.144222847202
146, 0.25214522485, 0.139721206044
147, 0.287968204391, 0.138882348861
151, 0.0, 0.1652861
152, 0.031365, 0.1652861
153, 0.06273, 0.1652861
154, 0.094095, 0.1652861
156, 0.124683249222, 0.165861357563
189, 0.156825, 0.1652861
163, 0.189978244061, 0.168552116135
193, 0.219555, 0.1652861
170, 0.251953134991, 0.166067888686
175, 0.31365, 0.1652861
179, 0.0, 0.1888984
178, 0.031365, 0.1888984
180, 0.06273, 0.1888984
181, 0.0948683944207, 0.186105991855
184, 0.12546, 0.1888984
185, 0.156825, 0.1888984
191, 0.185226863639, 0.189033619679
192, 0.21459450643, 0.190219519385
196, 0.248868626255, 0.189540639649
197, 0.277613280215, 0.188996385139
200, 0.31365, 0.188840621897
201, 0.0, 0.2125107
202, 0.031365, 0.2125107
203, 0.061560970472, 0.211435761567
206, 0.094095, 0.2125107
208, 0.123520746115, 0.217288884383
212, 0.156824305885, 0.210136954906
215, 0.19080765368, 0.21182785412
216, 0.219555, 0.2125107
217, 0.24652142594, 0.211374186361
219, 0.282347639617, 0.2121001649
220, 0.31365, 0.213088792923
222, 0.0, 0.236123
221, 0.031365, 0.236123
224, 0.06273, 0.236123
226, 0.094095, 0.236123
230, 0.12546, 0.236123
236, 0.156825, 0.236123
237, 0.18819, 0.236123
238, 0.219555, 0.236123
246, 0.25092, 0.236123
239, 0.282285, 0.236123
244, 0.31365, 0.236123
2, 0.0150409063102, 0.0175241129844
5, 0.0, 0.0143072034694
3, 0.0156825, 0.0
11, 0.0944401447071, 0.0170218543566
12, 0.108202138276, 0.0142655596685
14, 0.1097775, 0.0236123
16, 0.1097775, 0.0
15, 0.12546, 0.01180615
19, 0.1411425, 0.01180615
20, 0.137460594687, 0.0
23, 0.157190514557, 0.0120435227667
21, 0.1411425, 0.0236123
24, 0.1725075, 0.01180615
26, 0.1725075, 0.0236123
28, 0.174079024203, 0.0
27, 0.18819, 0.01180615
30, 0.20326319059, 0.0099544646677
31, 0.203137388716, 0.0
34, 0.217996940452, 0.0162543067349
32, 0.201648730844, 0.0253407563922
36, 0.25092, 0.01180615
39, 0.265091546036, 0.0115111489103
40, 0.265188659994, 0.0
43, 0.2979675, 0.0132903239577
42, 0.269085418102, 0.0219175315684
49, 0.14331614143, 0.0389180302184
51, 0.156825, 0.03541845
52, 0.170034925911, 0.0302540838763
55, 0.187241635364, 0.0308634812215
53, 0.170973146505, 0.0466686190136
57, 0.231749688431, 0.0405121944142
t60, 0.250420691759, 0.0399644155193
58, 0.234883810317, 0.0488399925217
61, 0.2666025, 0.03541845
63, 0.261841754198, 0.0453458988094
64, 0.283647280911, 0.0331311643709
67, 0.00997528838099, 0.059251007707
70, 0.026431195612, 0.0585429396298
69, 0.0156825, 0.0708369
72, 0.0470475, 0.0708369
76, 0.1411425, 0.0708369
104, 0.172905294963, 0.0687591528112
78, 0.189370395463, 0.0578988574842
82, 0.262145728385, 0.0567820182347
84, 0.282965777504, 0.0591653776972
83, 0.265510384054, 0.0711987638019
85, 0.0139070458991, 0.0765358149114
88, 0.031365, 0.08264305
89, 0.0463012651661, 0.0875761693955
91, 0.0595508652878, 0.083794762795
90, 0.0521108640296, 0.100487743127
93, 0.0784125, 0.08264305
95, 0.0759417426685, 0.094062139992
97, 0.125079439202, 0.0851866737525
96, 0.113088578118, 0.0953941670146
99, 0.143620352067, 0.0847078895557
100, 0.141046495561, 0.096007596875
102, 0.155098055673, 0.0796347264443
103, 0.172456234293, 0.0843806927744
106, 0.192866733648, 0.0862017124287
105, 0.175478179057, 0.0961076917822
108, 0.206360054844, 0.0816320301498
110, 0.203328294152, 0.0983508749474
112, 0.265850130274, 0.0841722754528
113, 0.283000506379, 0.0840718733578
114, 0.303036176529, 0.0912194545513
121, 0.111243800079, 0.102959886527
123, 0.12546, 0.10625535
124, 0.145186532288, 0.106177357559
126, 0.156825, 0.10625535
127, 0.177405333868, 0.10909704185
129, 0.1725075, 0.1180615
130, 0.18819, 0.10625535
133, 0.284435184261, 0.103796377701
136, 0.300997320603, 0.104240413217
135, 0.31365, 0.109425841822
141, 0.111351058915, 0.124763865227
143, 0.122330041541, 0.130632700189
142, 0.112021299889, 0.141456280139
243, 0.156825, 0.12986765
166, 0.174585056762, 0.136596948305
148, 0.2979675, 0.12986765
149, 0.31365, 0.132737078008
150, 0.302337133041, 0.140742193464
155, 0.110958484372, 0.156488764047
157, 0.123506443041, 0.151563704181
158, 0.138959817302, 0.151649897722
161, 0.158211487404, 0.158307767024
159, 0.140308231574, 0.169185557451
162, 0.174346818609, 0.14887558308
164, 0.176749514131, 0.168899157395
165, 0.18819, 0.15347995
167, 0.2038725, 0.15347995
169, 0.218436569511, 0.154768432689
168, 0.20646420232, 0.163454153961
172, 0.2352375, 0.15347995
171, 0.2352375, 0.1652861
174, 0.282285, 0.15347995
173, 0.299052995126, 0.151252115601
176, 0.296157702422, 0.164094288997
177, 0.31365, 0.147922235167
183, 0.12546, 0.17709225
182, 0.115020521952, 0.189065620836
187, 0.143150811124, 0.176461278361
186, 0.138367010451, 0.187435512478
188, 0.156825, 0.17709225
190, 0.174773303996, 0.179131868025
194, 0.223398006386, 0.175796432928
195, 0.2352375, 0.17709225
198, 0.283410759935, 0.175681933912
199, 0.295448251155, 0.176846292296
204, 0.0922385576628, 0.201463078266
205, 0.078759504717, 0.208169638245
207, 0.107329430815, 0.199650268966
209, 0.107340529686, 0.214649306608
210, 0.126314562813, 0.196624227919
211, 0.137598251382, 0.200265546052
213, 0.143355236652, 0.21294448339
214, 0.1725075, 0.2125107
218, 0.263507995658, 0.208989696919
223, 0.06273, 0.22431685
225, 0.0720254659882, 0.221339169391
227, 0.0784125, 0.236123
228, 0.0983042590694, 0.222777379368
229, 0.107905816156, 0.231588924845
231, 0.128500216507, 0.22426970333
232, 0.1411425, 0.22431685
233, 0.145352032138, 0.236123
234, 0.160670487099, 0.221709277836
235, 0.173524670602, 0.224567595595
241, 0.265582570598, 0.221943044414
240, 0.264044860278, 0.236123
242, 0.2979675, 0.224605896461
** The OOF2 element type is T3_3. The type provided for ABAQUS is only
a guess
** and may have to be modified by the user to be meaningful.
*ELEMENT, TYPE=CPS3
1, 1, 2, 3
2, 4, 5, 2
3, 6, 3, 5
4, 2, 5, 3
5, 2, 7, 4
6, 2, 1, 7
7, 1, 8, 7
8, 8, 1, 9
9, 10, 8, 9
10, 11, 8, 10
11, 10, 12, 11
12, 13, 14, 12
13, 12, 14, 11
14, 13, 12, 15
15, 10, 16, 12
16, 17, 15, 16
17, 12, 16, 15
18, 18, 19, 20
19, 17, 20, 15
20, 19, 15, 20
21, 13, 19, 21
22, 22, 21, 23
23, 19, 23, 21
24, 18, 24, 23
25, 25, 26, 24
26, 22, 23, 26
27, 24, 26, 23
28, 25, 24, 27
29, 18, 28, 24
30, 29, 27, 28
31, 24, 28, 27
32, 25, 27, 30
33, 30, 27, 31
34, 25, 30, 32
35, 33, 34, 30
36, 35, 32, 34
37, 36, 33, 37
38, 38, 39, 40
39, 41, 36, 39
40, 37, 40, 36
41, 39, 36, 40
42, 41, 39, 42
43, 38, 43, 39
44, 39, 43, 42
45, 4, 44, 45
46, 44, 4, 7
47, 8, 44, 7
48, 44, 8, 46
49, 8, 47, 46
50, 14, 13, 47
51, 47, 13, 48
52, 49, 48, 13
53, 49, 50, 48
54, 50, 49, 51
55, 13, 21, 49
56, 22, 51, 21
57, 49, 21, 51
58, 50, 52, 53
59, 54, 53, 55
60, 55, 56, 54
61, 32, 35, 56
62, 57, 56, 35
63, 56, 57, 58
64, 59, 58, 60
65, 57, 60, 58
66, 41, 61, 60
67, 62, 63, 61
68, 59, 60, 63
69, 61, 63, 60
70, 62, 61, 64
71, 41, 42, 61
72, 61, 42, 64
73, 64, 65, 62
74, 62, 65, 66
75, 67, 45, 44
76, 67, 68, 45
77, 68, 67, 69
78, 44, 70, 67
79, 71, 69, 70
80, 67, 70, 69
81, 72, 71, 70
82, 72, 70, 44
83, 47, 73, 46
84, 73, 47, 74
85, 47, 75, 74
86, 75, 47, 48
87, 50, 75, 48
88, 76, 50, 77
89, 76, 75, 50
90, 78, 56, 79
91, 78, 54, 56
92, 79, 56, 80
93, 56, 81, 80
94, 58, 81, 56
95, 58, 59, 81
96, 59, 82, 81
97, 59, 63, 82
98, 63, 62, 82
99, 81, 82, 83
100, 62, 84, 82
101, 82, 84, 83
102, 85, 86, 68
103, 85, 87, 86
104, 87, 85, 88
105, 68, 69, 85
106, 73, 89, 72
107, 87, 88, 89
108, 71, 72, 88
109, 89, 88, 72
110, 87, 89, 90
111, 73, 91, 89
112, 92, 90, 91
113, 89, 91, 90
114, 73, 93, 91
115, 94, 95, 93
116, 92, 91, 95
117, 93, 95, 91
118, 93, 74, 94
119, 93, 73, 74
120, 75, 94, 74
121, 75, 96, 94
122, 75, 97, 96
123, 97, 98, 96
124, 75, 99, 97
125, 98, 97, 100
126, 99, 100, 97
127, 101, 99, 102
128, 75, 76, 99
129, 77, 102, 76
130, 99, 76, 102
131, 79, 103, 104
132, 101, 102, 103
133, 77, 104, 102
134, 101, 103, 105
135, 79, 106, 103
136, 107, 105, 106
137, 79, 108, 106
138, 109, 110, 108
139, 107, 106, 110
140, 108, 110, 106
141, 108, 80, 109
142, 108, 79, 80
143, 81, 109, 80
144, 109, 81, 111
145, 112, 111, 81
146, 81, 83, 112
147, 112, 83, 113
148, 114, 115, 116
149, 87, 117, 86
150, 117, 87, 118
151, 87, 119, 118
152, 90, 119, 87
153, 90, 92, 119
154, 95, 119, 92
155, 95, 94, 119
156, 119, 94, 120
157, 121, 120, 94
158, 121, 122, 120
159, 122, 121, 123
160, 94, 96, 121
161, 98, 123, 96
162, 121, 96, 123
163, 101, 124, 100
164, 122, 123, 124
165, 98, 100, 123
166, 124, 123, 100
167, 122, 124, 125
168, 124, 101, 126
169, 124, 126, 125
170, 101, 127, 126
171, 128, 129, 127
172, 125, 126, 129
173, 127, 129, 126
174, 128, 127, 130
175, 101, 105, 127
176, 107, 130, 105
177, 127, 105, 130
178, 128, 130, 110
179, 130, 107, 110
180, 109, 131, 132
181, 131, 109, 111
182, 133, 134, 131
183, 116, 135, 114
184, 136, 114, 135
185, 117, 137, 138
186, 137, 117, 118
187, 119, 137, 118
188, 137, 119, 139
189, 119, 140, 139
190, 140, 119, 120
191, 141, 120, 122
192, 141, 140, 120
193, 140, 141, 142
194, 122, 143, 141
195, 141, 143, 142
196, 128, 144, 145
197, 144, 128, 132
198, 131, 144, 132
199, 144, 131, 146
200, 131, 147, 146
201, 147, 131, 134
202, 148, 149, 150
203, 137, 151, 138
204, 151, 137, 152
205, 137, 153, 152
206, 153, 137, 139
207, 140, 153, 139
208, 153, 140, 154
209, 155, 154, 140
210, 155, 156, 154
211, 156, 155, 157
212, 140, 142, 155
213, 155, 142, 157
214, 158, 156, 157
215, 156, 158, 159
216, 160, 161, 158
217, 158, 161, 159
218, 160, 162, 161
219, 163, 164, 162
220, 163, 162, 165
221, 145, 165, 166
222, 162, 166, 165
223, 144, 167, 145
224, 167, 163, 165
225, 167, 165, 145
226, 163, 167, 168
227, 167, 169, 168
228, 170, 171, 172
229, 172, 146, 170
230, 172, 144, 146
231, 147, 173, 174
232, 175, 176, 173
233, 173, 176, 174
234, 175, 173, 177
235, 173, 150, 177
236, 151, 178, 179
237, 178, 151, 152
238, 153, 178, 152
239, 178, 153, 180
240, 153, 181, 180
241, 181, 153, 154
242, 156, 181, 154
243, 156, 182, 181
244, 156, 183, 182
245, 183, 184, 182
246, 185, 186, 187
247, 184, 183, 186
248, 187, 186, 183
249, 185, 187, 188
250, 189, 188, 159
251, 187, 159, 188
252, 163, 190, 164
253, 185, 188, 190
254, 189, 164, 188
255, 190, 188, 164
256, 190, 191, 185
257, 190, 163, 191
258, 163, 192, 191
259, 163, 168, 192
260, 168, 193, 194
261, 168, 194, 192
262, 170, 195, 171
263, 192, 194, 195
264, 193, 171, 194
265, 195, 194, 171
266, 195, 196, 192
267, 195, 170, 196
268, 170, 197, 196
269, 198, 197, 170
270, 175, 199, 176
271, 197, 198, 199
272, 199, 198, 176
273, 199, 200, 197
274, 199, 175, 200
275, 178, 201, 179
276, 201, 178, 202
277, 178, 203, 202
278, 203, 178, 180
279, 181, 204, 205
280, 204, 206, 205
281, 181, 207, 204
282, 208, 209, 207
283, 206, 204, 209
284, 207, 209, 204
285, 208, 207, 210
286, 181, 182, 207
287, 184, 210, 182
288, 207, 182, 210
289, 185, 211, 186
290, 208, 210, 211
291, 184, 186, 210
292, 211, 210, 186
293, 212, 211, 185
294, 212, 213, 211
295, 213, 208, 211
296, 214, 185, 215
297, 214, 212, 185
298, 215, 185, 191
299, 192, 215, 191
300, 215, 192, 216
301, 192, 217, 216
302, 217, 192, 196
303, 218, 197, 219
304, 197, 220, 219
305, 220, 197, 200
306, 201, 221, 222
307, 221, 201, 202
308, 223, 224, 221
309, 203, 225, 223
310, 226, 227, 225
311, 224, 223, 227
312, 225, 227, 223
313, 226, 225, 228
314, 203, 205, 225
315, 206, 228, 205
316, 225, 205, 228
317, 208, 229, 209
318, 226, 228, 229
319, 206, 209, 228
320, 229, 228, 209
321, 226, 229, 230
322, 229, 208, 231
323, 229, 231, 230
324, 208, 232, 231
325, 230, 231, 233
326, 232, 233, 231
327, 208, 213, 232
328, 232, 213, 234
329, 215, 235, 214
330, 236, 234, 235
331, 212, 214, 234
332, 235, 234, 214
333, 235, 237, 236
334, 235, 215, 237
335, 215, 238, 237
336, 238, 215, 216
337, 217, 238, 216
338, 239, 240, 241
339, 239, 241, 242
340, 217, 218, 241
341, 219, 242, 218
342, 241, 218, 242
343, 242, 219, 220
344, 84, 115, 113
345, 113, 83, 84
346, 159, 183, 156
347, 159, 187, 183
348, 243, 160, 143
349, 177, 150, 149
350, 32, 55, 25
351, 32, 56, 55
352, 113, 115, 114
353, 143, 157, 142
354, 160, 158, 143
355, 158, 157, 143
356, 52, 22, 26
357, 52, 51, 22
358, 218, 196, 197
359, 218, 217, 196
360, 36, 34, 33
361, 36, 41, 34
362, 128, 166, 129
363, 160, 243, 166
364, 166, 243, 129
365, 145, 166, 128
366, 110, 132, 128
367, 110, 109, 132
368, 198, 170, 174
369, 174, 176, 198
370, 34, 57, 35
371, 34, 41, 57
372, 223, 202, 203
373, 223, 221, 202
374, 47, 8, 11
375, 14, 47, 11
376, 84, 66, 115
377, 84, 62, 66
378, 205, 180, 181
379, 205, 203, 180
380, 72, 46, 73
381, 72, 44, 46
382, 125, 143, 122
383, 125, 243, 143
384, 133, 131, 111
385, 133, 114, 136
386, 113, 114, 133
387, 112, 133, 111
388, 242, 244, 239
389, 242, 220, 244
390, 43, 245, 65
391, 43, 38, 245
392, 241, 240, 246
393, 246, 238, 217
394, 217, 241, 246
395, 64, 43, 65
396, 43, 64, 42
397, 26, 55, 52
398, 26, 25, 55
399, 85, 71, 88
400, 85, 69, 71
401, 174, 146, 147
402, 174, 170, 146
403, 50, 104, 77
404, 79, 104, 78
405, 50, 53, 104
406, 104, 53, 78
** Point boundaries in OOF2
*NSET, NSET=topleft
222
*NSET, NSET=bottomleft
6
*NSET, NSET=topright
244
*NSET, NSET=bottomright
245
** Edge boundaries in OOF2
*NSET, NSET=top
244, 239, 240, 246, 238, 237, 236, 233, 230, 226, 227, 224, 221, 222
*NSET, NSET=right
245, 65, 66, 115, 116, 135, 149, 177, 175, 200, 220, 244
*NSET, NSET=left
222, 201, 179, 151, 138, 117, 86, 68, 45, 4, 5, 6
*NSET, NSET=bottom
6, 3, 1, 9, 10, 16, 17, 20, 18, 28, 29, 31, 33, 37, 40, 38,
245
*ELSET, ELSET=C
21, 55, 57, 77, 105, 109, 111, 115, 117, 160, 161, 162, 165, 212, 213,
232,
271, 272, 290, 291, 292, 399, 400
*SOLID SECTION, ELSET=C, MATERIAL=C
*ELSET, ELSET=W
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 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, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 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, 106, 107, 108, 110, 112, 113, 114, 116, 118, 119,
120, 121,
122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
136, 137,
138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151,
152, 153,
154, 155, 156, 157, 158, 159, 163, 164, 166, 167, 168, 169, 170, 171,
172, 173,
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187,
188, 189,
190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
204, 205,
206, 207, 208, 209, 210, 211, 214, 215, 216, 217, 218, 219, 220, 221,
222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 233, 234, 235, 236, 237, 238,
239, 240,
241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254,
255, 256,
257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270,
273, 274,
275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288,
289, 293,
294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307,
308, 309,
310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323,
324, 325,
326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339,
340, 341,
342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355,
356, 357,
358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371,
372, 373,
374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387,
388, 389,
390, 391, 392, 393, 394, 395, 396, 397, 398, 401, 402, 403, 404, 405,
406
*SOLID SECTION, ELSET=W, MATERIAL=W
*MATERIAL, NAME=C
** Use the information in the header to complete these fields under
MATERIAL
*MATERIAL, NAME=W
** Use the information in the header to complete these fields under
MATERIAL
 
R

Rhodri James

Hello,
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

Cutting and pasting (and substituting the sample file for `path`),
it works for me on Python 2.6.2 (Ubuntu Linux). It must be something
to do with the context you're calling the code in. Are you checking
the output before the program has finished running or something?
 
D

Dave Angel

Alexzive said:
Hello,
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

Many thanks for your help!
Alex

the original file to crop (in path) looks like this:

<snip>
Is there a reason you used e-1 instead of e in the slice? That's going
to chop off the last newline. And in some (broken) text viewers, you
might not see the partial line. Also, it'll behave a bit different in
Windows than in one of the Unix variants, due to newline being
represented by two characters instead of one.

e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
 
P

Peter Otten

Alexzive said:
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:

linestring = open(path, 'r').read() #read all the inp file in
linestring

i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")

when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

Do you check the contents of the resulting file by feeding it to another
program?
53, 0.170973146505, 0.0466686190136
57, 0.231749688431, 0.0405121944142
t60, 0.250420691759, 0.0399644155193
58, 0.234883810317, 0.0488399925217
61, 0.2666025, 0.03541845

There's a "t" at the start of the line after the last line you claim to see,
so maybe your input file is corrupt and your reader stumbles over that line.

Peter
 
A

Alessandro

Alexzive said:
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:
linestring = open(path, 'r').read() #read all the inp file in
linestring
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)
unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")
when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?

Do you check the contents of the resulting file by feeding it to another
program?
53, 0.170973146505, 0.0466686190136
57, 0.231749688431, 0.0405121944142
t60, 0.250420691759, 0.0399644155193
58, 0.234883810317, 0.0488399925217
61, 0.2666025, 0.03541845

There's a "t" at the start of the line after the last line you claim to see,
so maybe your input file is corrupt and your reader stumbles over that line.

Peter

Thank you all for replying.

- yes, I oversaw the "t" at "60, 0.250420691759, 0.0399644155193". I
then removed it for the following tests
- changing from "e-1" to "e" apparently solved the problem, BUT I then
realized it was just because a second access to the file (still open)
seems to be the real solution --> after Nfile.close() e retrying with
"e" instead of "e-1" the problem is still there.
- I also tryied to inizialize e and i to zero --> nothing changes
- until now, the only solution which works is to repeat the code while
the file is still open, which means a quite redundant code:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

Alex
 
P

Peter Otten

Alessandro said:
- until now, the only solution which works is to repeat the code while
the file is still open, which means a quite redundant code:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

Is this the complete script? The only effect of the second copy of your code
above is that it implicitly closes the first Nfile. Otherwise it is cargo
cult.

Peter
 
J

John Machin

Alexzive said:
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:
linestring = open(path, 'r').read() #read all the inp file in
linestring
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)
unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")
when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?
Do you check the contents of the resulting file by feeding it to another
program?
There's a "t" at the start of the line after the last line you claim to see,
so maybe your input file is corrupt and your reader stumbles over that line.

Thank you all for replying.

- yes, I oversaw the "t" at "60, 0.250420691759, 0.0399644155193". I
then removed it for the following tests
- changing from "e-1" to "e" apparently solved the problem, BUT I then
realized it was just because a second access to the file (still open)
seems to be the real solution --> after Nfile.close() e retrying with
"e" instead of "e-1" the problem is still there.
- I also tryied to inizialize e and i to zero --> nothing changes
- until now, the only solution which works is to repeat the code while
the file is still open, which means a quite redundant code:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)

Insert here:
Nfile.close()

Remove the redundant second pass.
linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)
 
A

Alessandro

Alexzive wrote:
I am a newby with python. I wrote the following code to extract a text
from a file and write it to another file:
linestring = open(path, 'r').read() #read all the inp file in
linestring
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")
Nfile.write(textN)
unfortunately when I check N.txt some lines are missing (it only crop
until "57, 0.231749688431, 0.0405121944142" but I espect the final
line to be "242, 0.2979675, 0.224605896461". I check textN and it has
all the lines until "242..")
when I try Nfile.write(textN) again it writes some more lines but
still not all!
what is wrong whit this?
Do you check the contents of the resulting file by feeding it to another
program?
53, 0.170973146505, 0.0466686190136
57, 0.231749688431, 0.0405121944142
t60, 0.250420691759, 0.0399644155193
58, 0.234883810317, 0.0488399925217
61, 0.2666025, 0.03541845
There's a "t" at the start of the line after the last line you claim to see,
so maybe your input file is corrupt and your reader stumbles over that line.
Peter
Thank you all for replying.
- yes, I oversaw the "t" at "60, 0.250420691759, 0.0399644155193". I
then removed it for the following tests
- changing from "e-1" to "e" apparently solved the problem, BUT I then
realized it was just because a second access to the file (still open)
seems to be the real solution --> after Nfile.close() e retrying with
"e" instead of "e-1" the problem is still there.
- I also tryied to inizialize e and i to zero --> nothing changes
- until now, the only solution which works is to repeat the code while
the file is still open, which means a quite redundant code:
linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1] # crop the ELement+nodes list
Nfile = open("N.txt", "w")


I closed and restarted the python console. Now this code (with added
"Nfile.close()" at the end) seems to work properly:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1]
Nfile = open("N.txt", "w")
Nfile.write(textN)
Nfile.close()

thanks, Alex
 
D

Dave Angel

Alessandro said:
<snip>

I closed and restarted the python console. Now this code (with added
"Nfile.close()" at the end) seems to work properly:

linestring = open(path, 'r').read()
i=linestring.index("*NODE")
i=linestring.index("E",i)
e=linestring.index("*",i+10)
textN = linestring[i+2:e-1]
Nfile = open("N.txt", "w")
Nfile.write(textN)
Nfile.close()

thanks, Alex
Others had already mentioned close(), but I didn't bother, since it
would be automatically closed when you exited the function, or finished
the script. I never dreamed you were running all this from the
interpreter. If so, why didn't you copy the prompts?

The close is best done explicitly, but it is implicit when the Nfile
goes out of scope, or gets reassigned (like the new open).

However, the bug I described is still there. Study the following, and
try it:

def test():
buf = "abcdefghijklmnopqrstg000"
i = buf.index("d")
e = buf.index("g", i+10)
buf2 = buf[i+2:e-1]
print buf2

running it produces the string:

fghijklmnopqrs

Notice the 't' is missing. If you're deliberately doing that, fine.
But I doubt it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top