How to refer a certain place in a line in Python

2024/10/11 10:22:00

I have this little piece of code:

g = open("spheretop1.stl", "r")
m = open("morelinestop1.gcode", "w")
searchlines = g.readlines()
file = ""
for i, line in enumerate(searchlines):if X1 in line and Y1 in line:m.write("start" + "\n")

with X1 = '206.9799' and Y1 = '0.1218346'

the line this file refers to looks like this:

  facet normal 4.650354e-002 -9.989174e-001 -1.217645e-003outer loopvertex 2.069799e+002 1.218346e-001 2.000000e+002vertex 2.069756e+002 1.218346e-001 1.997564e+002vertex 2.139428e+002 4.871899e-001 1.995131e+002endloopendfacet

I basically only want the file to write "start" + "\n" when the X1 and Y1 are in the same line AND are the first two variables in that line, as in the 3rd line above. So what I want to do is find X1 in the line at position x (17 spaces from left) and Y1 in the line at position y (31 spaces from left). Hope that its clear :)

Answer

I would use regular expressions. Live example.

import reX1 = 206.9799
Y1 = 0.1218346for i, line in enumerate(lines):r = re.match(r'^\s*vertex (\d+\.\d+e[-+]\d+) (\d+\.\d+e[-+]\d+) \d+\.\d+e[-+]\d+\s*$', line)if r and X1 == float(r.group(1)) and Y1 == float(r.group(2)):m.write("start" + "\n") 

Be aware that comparing floats can be imprecise. You have to decide how much imprecision you're willing to accept when comparing values. You can use a function like this to compare two floats within a certain amount of precision.

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
https://en.xdnf.cn/q/118333.html

Related Q&A

List comprehension output is None [duplicate]

This question already has answers here:Python list comprehension: list sub-items without duplicates(6 answers)Closed 8 years ago.Im new to python and I wanted to try to use list comprehension but outco…

error while inserting into mysql from python for loop [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:convert list to string to insert into my sql in one row in python scrapy Is this script correct. I want to insert the scra…

Half of Matrix Size Missing

I wanted to ask why is it that some of my matrices in Python are missing a size value? The matrices/arrays that re defined show the numbers in them, but the size is missing and it is throwing me other…

Clicking Side Panel Elements in Selenium Without IFrames

I want to download U.S. Department of Housing and Urban Development data using Pythons Selenium. Heres my code. import os from selenium import webdriver from webdriver_manager.chrome import ChromeDrive…

Library to extract data from open Excel workbooks

I am trying to extract data from workbooks that are already open. I have found the xlrd library, but it appears you can only use this with workbooks you open through Python. The workbooks I will use in…

Keras apply different Dense layer to each timestep

I have training data in the shape of (-1, 10) and I want to apply a different Dense layer to each timestep. Currently, I tried to achieve this by reshaping input to (-1, 20, 1) and then using a TimeDis…

Create a pass-through for an installed module to achieve an optional import

Im writing a library in python 3.7. Id like it to have as few dependencies as possible. For example, tqdm is nice to have but ideally Id like my library to work without it if its not there. Therefore, …

Django, Redis: Where to put connection-code

I have to query redis on every request in my Django-app. Where can I put the setup/ connection routine (r = redis.Redis(host=localhost, port=6379)) so that I can access and reuse the connection without…

Events and Bindings in tkinter does not work in loop

I am trying to create binding in a loop using tkinter module.from tkinter import * class Gui(Frame):def __init__(self, parent):Frame.__init__(self, parent) self.parent = parentself.initUI()def Arrays(…

Python Machine Learning Algorithm to Recognize Known Events

I have two sets of data. These data are logged voltages of two points A and B in a circuit. Voltage A is the main component of the circuit, and B is a sub-circuit. Every positive voltage in B is (1) co…