arrays - "Value error: Mixing iteration and read methods would lose data "message while extracting numbers from a string from a .txt file using python -


i have program grabs numbers .txt file , puts array. problem numbers not isolated or ordered. .txt file looks this:

g40 z=10 a=30 x10 y50 a=30 x40 y15 a=50 x39 y14 g40 z=11 a=30 x10 y50 a=30 x40 y15 a=50 x39 y14 

the output should new .txt file has following array format

x y z 10 50 10 40 15 10 39 14 10 10 50 11 40 15 11 39 14 11 

this have done far, although i'm not sure how write output new file...

inputfile = open('circletest1.gcode' , 'r') def find_between( s, first, last ):       try:           start = s.index( first ) + len( first )           end = s.index( last, start )           return s[start:end]       except valueerror:           return "" in range(203): inputfile.next() # skip first 203 lines while true:     my_text = inputfile.readline()     z = find_between(my_text, "z =", " ")     x = find_between(my_text, "x", " ")     y = find_between(my_text, "y", " ")     print(x ," ", y, " ", z)     if not my_text:break  inputfile.close() 

for while receiving indentation errors, believe have fixed problem. error message getting "value error: mixing iteration , read methods lose data".

i not sure go here, nor sure how import results separate new txt file.

also, in code, there way preserve z value outside of loop until new z value assigned?

if understand correctly, want combine z value lines start g x , y values following lines (until next g line).

if so, i'd use single loop, printing on lines start a , saving new z value on lines start g. i'd line parsing regex, use simple string manipulation if wanted (i'd split , skip first letter or letters of relevant items).

import itertools import re  z = none  open('circletest1.gcode') input_file:     line in itertools.islice(203, none):  # islice skip first 203 lines         if line.startswith("g"):             z = re.search(r"z=(\d+)", line).group(1)         elif line.startswith("a"):             x, y = re.search(r"x(\d+) y(\d+)", line).groups()             print(x, y, z) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -