提问人:Захарцев Андрей 提问时间:11/12/2023 更新时间:11/12/2023 访问量:54
我需要创建一个正则表达式模板
I need to create a regular expression template
问:
我正在尝试通过模式在 rvf 文件中查找行,point_match、point2_match 和 point3_match 正在寻找这些行,point4_match输出值 None
需要最终确定的代码
import re
import tkinter as tk
from tkinter import ttk, Tk, filedialog
root = Tk()
root.title("Коррекция графических примитивов")
root.geometry("600x200")
root.resizable(width=False, height=True)
rvf_data = ''
output_directory = ''
# Функция для обработки текстового объекта и обновления координат
def process_vector_object_text(text):
point_match = re.search(r'<geom>POINT \((.*?) (.*?) (.*?)\)</geom>', text)
print(point_match)
point3_match = re.search(r'<prop>(\d*,\d*,\d*,\d*)</prop>', text)
print(point3_match)
point2_match = re.search(r'<reference_size>POINT2\((.*?)e\+(.*?),(.*?)e\+(.*?)\)</reference_size>', text)
print(point2_match)
point4_match = re.search(r'<geom>LINESTRING \((.*?) (.*?),(.*?) (.*?),(.*?) (.*?)\)</geom>', text)
print(point4_match)
return text
def open_file():
input_file_path = filedialog.askopenfilename(title="Выберите входной файл", filetypes=[("Файлы RVF", "*.rvf")])
filename_entry.delete(0, "end") # Очистить поле ввода
filename_entry.insert(0, input_file_path) # Вставить путь к выбранному файлу в поле ввода
with open(input_file_path, 'r', encoding='utf-8') as input_file:
global rvf_data
rvf_data = input_file.read()
def save_file():
global output_directory
output_directory = filedialog.askdirectory(title="Выберите каталог для сохранения")
filename_entry1.delete(0, "end") # Очистить поле ввода
filename_entry1.insert(0, output_directory) # Вставить путь к выбранному файлу в поле ввода
def final():
global output_file_path
vector_object_texts = re.findall(r'<obj type="vector_object_text" ID="\d+">(.*?)</obj>', rvf_data, re.DOTALL)
updated_rvf_data = rvf_data
# Обработка и обновление текстовых объектов
for text in vector_object_texts:
updated_text = process_vector_object_text(text)
if updated_text:
updated_rvf_data = updated_rvf_data.replace(text, updated_text)
# Если данные были обновлены, сохраняем обновленный файл RVF
if updated_rvf_data != rvf_data:
output_file_path = f'{output_directory}/updated_rvf.rvf'
with open(output_file_path, 'w', encoding='utf-8') as output_file:
output_file.write(updated_rvf_data)
print(f"Обновленный RVF файл сохранен как '{output_file_path}'.")
else:
print("Нет данных для обновления RVF файла.")
open_button = ttk.Button(text="Открыть директорию", command=open_file)
open_button.pack(padx=6, pady=40)
open_button.place(x=65, y=30)
filename_entry = tk.Entry(root, width=50)
filename_entry.pack()
filename_entry.place(x=250, y=32)
save_button = ttk.Button(text="Выбрать директорию для сохранения", command=save_file)
save_button.pack(padx=6, pady=40)
save_button.place(x=20, y=60)
filename_entry1 = tk.Entry(root, width=50)
filename_entry1.pack()
filename_entry1.place(x=250, y=62)
star_button = ttk.Button(text="Выполнить корректировку", command=final)
star_button.pack(padx=6, pady=40)
star_button.place(x=45, y=90)
label = tk.Label(root, text="", font=("Arial", 14))
label.pack()
root.mainloop()
我要在其中查找行的文本
<?xml version="1.0" encoding="UTF-8"?>
<document version="2" type="PAPER_VIEW" catalogue_link_record="false" catalogue_index="1">
<app_build_revision>21559_e65b8775\411M</app_build_revision>
<name>Новый документ</name>
<name_is_set>yes</name_is_set>
<metadata>
<mdi key="Document common page inside margins mm" type="QString">10,10,10,10</mdi>
<mdi key="Document common page outside margins mm" type="QString">5,5,5,5</mdi>
<mdi key="Document common paper size mm" type="QSizeF">@SizeF(420,297)</mdi>
<mdi key="PAPER_VIEW_MAIN_DOCUMENT" type="bool">true</mdi>
<mdi key="Printer resolution" type="int">300</mdi>
</metadata>
<documents>
<document version="2" type="PAPER_AREA" catalogue_link_record="false" catalogue_index="2">
<name>Новая страница 1</name>
<name_is_set>yes</name_is_set>
<vectors version="2" creator="tovi0" type="vector_layers">
<layer layer_id="8" version="2" is_visible="true" user_editable="false" user_removable="false" type="vector_layer" name="frames" catalogue_index="4">
<style>@Pen(#000000FF,0,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,36,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<shown_semantics>
<alignment>132</alignment>
<format_list/>
</shown_semantics>
<last_inserted_id>1</last_inserted_id>
<obj type="vector_object_paper_frame" ID="1">
<solid>0</solid>
<geom>POLYGON ((5 5,415 5,415 292,5 292))</geom>
<framed_document_catalogue_index>3</framed_document_catalogue_index>
<paper_frame_view_pos_x>6.0776733530845081e+02</paper_frame_view_pos_x>
<paper_frame_view_pos_y>2.6985404812972973e+02</paper_frame_view_pos_y>
<paper_frame_view_basic_scale_factor>5.5380360355328273e-01</paper_frame_view_basic_scale_factor>
<paper_frame_view_rotation>0</paper_frame_view_rotation>
<paper_frame_view_flip>0</paper_frame_view_flip>
<paper_frame_view_factory_name>simple</paper_frame_view_factory_name>
<paper_frame_name></paper_frame_name>
<show_mapscale>0</show_mapscale>
<mapscale_format>0</mapscale_format>
<linked_frames/>
</obj>
<sublayers/>
</layer>
<layer layer_id="9" version="2" is_visible="true" user_editable="true" user_removable="true" type="vector_layer" name="новый_слой" catalogue_index="5">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<shown_semantics>
<alignment>132</alignment>
<format_list/>
</shown_semantics>
<last_inserted_id>21</last_inserted_id>
<obj type="vector_object_contour" ID="1">
<style>@Pen(#FFFFFFFF,3,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<solid>0</solid>
<geom>POLYGON ((245.309555930931 283.485031456457,264.271361486487 264.523225900901,256.333861486487 255.262809234234,268.681083708709 243.797531456457,257.215805930931 218.66211478979,250.601222597598 207.196837012012,229.875528153153 216.898225900901,221.938028153153 200.582253678679,276.618583708709 174.123920345345,290.288722597598 204.551003678679,307.486639264264 212.488503678679,317.629000375376 237.623920345345,329.094278153153 243.797531456457,326.007472597598 186.030170345345,316.306083708709 148.988503678679,293.816500375376 111.064892567568,251.04219481982 85.0475314564566,200.771361486487 64.7628092342344,193.715805930931 88.1343370120122,189.747055930931 105.773225900901,133.743583708709 98.7176703453455,112.13594481982 112.387809234234,144.326917042042 160.453781456457,157.115111486487 205.432948123123,179.163722597598 253.939892567568,198.125528153153 284.807948123123,212.677611486487 279.516281456457,236.931083708709 276.870448123123))</geom>
</obj>
<obj type="vector_object_contour" ID="2">
<style>@Pen(#FFFFFFFF,2,2,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<solid>0</solid>
<geom>POLYGON ((256.157472597598 250.178399512012,265.470805930931 243.828399512012,247.479139264265 214.830066178679,238.165805930931 219.698399512012))</geom>
</obj>
<obj type="vector_object_polyline" ID="4">
<style>@Pen(#FFFFFFFF,1,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<geom>LINESTRING (161.83078219618 211.499843956457,153.314460097598 201.028163690477,130.744057580237 201.078566815477)</geom>
</obj>
<obj type="vector_object_text" ID="5">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="склад стали"/>
<prop>30,260,1,1</prop>
<geom>POINT (130.625028153153 195.50842728979 0)</geom>
<reference_size>POINT2(2.2489583333333329e+01,4.7624999999999993e+00)</reference_size>
</obj>
<obj type="vector_object_text" ID="6">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="МАШИНОСТРОИТЕЛЬНЫЙ ЗАВОД
РОССИЯ, КЕМЕРОВСКАЯ ОБЛАСТЬ, ЛЕНИНСК-КУЗНЕЦКИЙ
СШ=54-40-11 ВД=86-10-24 Н=223 М
13.10.2023"/>
<prop>30,516,1,1</prop>
<geom>POINT (210 15 0)</geom>
<reference_size>POINT2(1.4604999999999998e+02,2.4077083333333327e+01)</reference_size>
</obj>
<obj type="vector_object_text" ID="7">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="Учебный
Экз. №1"/>
<prop>30,260,1,1</prop>
<geom>POINT (388.184801246783 15.0 0)</geom>
<reference_size>POINT2(1.6933333333333330e+01,9.7895833333333311e+00)</reference_size>
</obj>
<obj type="vector_object_text" ID="8">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="Принадлежит и используется 
ООО «Завод Красный Октябрь» "/>
<prop>30,260,1,1</prop>
<geom>POINT (10.0673926721378 277.242874603175 0)</geom>
<reference_size>POINT2(6.3764583333333320e+01,9.7895833333333311e+00)</reference_size>
</obj>
<obj type="vector_object_text" ID="9">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="в 1 см 80 м"/>
<prop>30,260,1,1</prop>
<geom>POINT (210 282 0)</geom>
<reference_size>POINT2(2.1695833333333329e+01,4.7624999999999993e+00)</reference_size>
</obj>
<obj type="vector_object_contour" ID="10">
<style>@Pen(#FFFFFFFF,2,2,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<solid>0</solid>
<geom>POLYGON ((176.957626764265 237.597108134921,211.882626764265 219.182108134921,210.824293430931 215.795441468254,219.714293430931 211.138774801588,198.547626764265 185.315441468254,158.754293430931 203.730441468254,174.840960097598 238.232108134921))</geom>
</obj>
<obj type="vector_object_text" ID="12">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="стоянка автомобилей"/>
<prop>30,260,1,1</prop>
<geom>POINT (265.562621323668 220.379682473322 0)</geom>
<reference_size>POINT2(4.1539583333333326e+01,4.7624999999999993e+00)</reference_size>
</obj>
<obj type="vector_object_polyline" ID="13">
<style>@Pen(#FFFFFFFF,1,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<geom>LINESTRING (260.145482059404 234.590828516866,265.648815392737 226.124161850199,306.994370948293 226.124161850199)</geom>
</obj>
<obj type="vector_object_contour" ID="14">
<style>@Pen(#FFFFFFFF,2,2,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<solid>0</solid>
<geom>POLYGON ((161.791352595118 194.890729310516,209.747081761785 171.408958477183,192.549165095118 138.99750014385,183.619477595118 142.635520977183,179.981456761785 136.02093764385,168.405935928451 140.651145977183,162.783540095118 129.406354310516,136.325206761785 142.635520977183,141.947602595118 153.88031264385))</geom>
</obj>
<obj type="vector_object_polyline" ID="15">
<style>@Pen(#FFFFFFFF,1,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<geom>LINESTRING (135.565852595118 141.643333477183,114.568519261785 131.82200014385,93.3154220395624 131.877456810516)</geom>
</obj>
<obj type="vector_object_text" ID="16">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="цех сборки
корпусов"/>
<prop>30,260,1,1</prop>
<geom>POINT (93.3154363038265 121.34042743364 0)</geom>
<reference_size>POINT2(2.0902083333333330e+01,9.7895833333333311e+00)</reference_size>
</obj>
<obj type="vector_object_contour" ID="17">
<style>@Pen(#FFFFFFFF,2,2,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<solid>0</solid>
<geom>POLYGON ((193.441261350078 104.947475337302,202.754594683412 67.6941420039688,248.262928016745 86.5324753373021,290.172928016745 113.202475337302,309.434594683412 144.317475337302,300.544594683412 148.762475337302,289.749594683412 133.310808670635,289.114594683412 139.025808670635,274.086261350078 135.427475337302,274.297928016745 140.930808670635,269.641261350078 141.142475337302,268.371261350078 145.375808670635,230.059594683412 138.602475337302,235.351261350078 114.260808670635,206.141261350078 110.027475337302,193.441261350078 104.947475337302))</geom>
</obj>
<obj type="vector_object_polyline" ID="19">
<style>@Pen(#FFFFFFFF,1,1,16,64)|@Brush(#000000FF,0)|@Font(Arial,14,-1,5,50,0,0,0,0,0)|font_color_is_default</style>
<geom>LINESTRING (283.856265516745 108.117183670635,289.139465516745 98.0926503373021,333.407432183412 98.1356892261911)</geom>
</obj>
<obj type="vector_object_text" ID="20">
<style>@Pen(#FF0000FF,0,1,16,64)|@Brush(#FFFFFFFF,1)|@Font(Arial,12,-1,5,50,0,0,0,0,0)|@Color(#000000FF)</style>
<sem key="text" type="QString" value="обрабатывающие цехи"/>
<prop>30,260,1,1</prop>
<geom>POINT (288.982168487238 92.4947277709422 0)</geom>
<reference_size>POINT2(4.4449999999999989e+01,4.7624999999999993e+00)</reference_size>
</obj>
<sublayers/>
</layer>
</vectors>
<metadata>
<mdi key="Document page has specific parameters" type="bool">false</mdi>
<mdi key="Document page rect mm" type="QRectF">@RectF(5,5,410,287)</mdi>
<mdi key="HGuideLineMoveStarted" type="">@Invalid()</mdi>
<mdi key="Total printable decart rect" type="QRectF">@RectF(5,5,410,287)</mdi>
<mdi key="VGuideLineMoveStarted" type="">@Invalid()</mdi>
<mdi key="act_context_menu::cursor_position" type="">@Invalid()</mdi>
<mdi key="act_hand_mouse::drag_started" type="bool">false</mdi>
<mdi key="act_move::last.x" type="double">320.002582183412</mdi>
<mdi key="act_move::last.y" type="double">95.9908003373022</mdi>
<mdi key="act_move::reference_point.x" type="double">161.83078219618</mdi>
<mdi key="act_move::reference_point.y" type="double">211.499843956457</mdi>
</metadata>
<documents>
<document version="2" type="wgt_raster_view" catalogue_link_record="false" catalogue_index="3">
<name>завод</name>
<name_is_set>no</name_is_set>
<rasters>
<raster>
<filename>d0/d0/r0/crop.hw2444.tiff</filename>
<visible>1</visible>
<Default_decart_to_layer_transform type="t_transform_affine">
<ForwardCoeff>
<m11>9.9954796738885165e-01</m11>
<m12>0</m12>
<m21>0</m21>
<m22>1</m22>
<dx>-1.6349260465290067e+02</dx>
<dy>0</dy>
</ForwardCoeff>
<InverseCoeff>
<m11>1.0004522370370370e+00</m11>
<m12>0</m12>
<m21>0</m21>
<m22>1</m22>
<dx>1.6356654206400637e+02</dx>
<dy>0</dy>
</InverseCoeff>
</Default_decart_to_layer_transform>
<properties>[FrameColor]@Color(#00FF00FF)[/FrameColor][clipper]@ByteArray(POLYGON ((0 0,888 0,888 545,0 545,0 0)))[/clipper][clipper_enabled]true[/clipper_enabled] </properties>
</raster>
</rasters>
<metadata>
<mdi key="HGuideLineMoveStarted" type="">@Invalid()</mdi>
<mdi key="PAPER_VIEW_FRAME_OBJECT_IDS" type="QStringList">@List(1)</mdi>
<mdi key="VGuideLineMoveStarted" type="">@Invalid()</mdi>
<mdi key="act_hand_mouse::drag_started" type="bool">false</mdi>
<mdi key="decart_to_image_mm_scale" type="double">0.211666666666667</mdi>
</metadata>
</document>
</documents>
</document>
</documents>
</document>
我输出这样的行,但是当输出point4_match输出值时 None
<。匹配对象;span=(215, 270), match='POINT (130.625028153153 195.50842728979 0)<> <。匹配对象;span=(185, 208), match='30,260,1,1'> <。匹配对象;span=(277, 363), match='<reference_size>POINT2(2.2489583333333329e+01,4.7> 没有 <。匹配对象;span=(330, 359), match='POINT (210 15 0)'> <。匹配对象;span=(300, 323), match='30,516,1,1'> <。匹配对象;span=(366, 452), match='<reference_size>POINT2(1.46049999999999998e+02,2.4> 没有 <。匹配对象;span=(223, 267), match='POINT (388.184801246783 15.0 0)'> <。匹配对象;span=(193, 216), match='30,260,1,1'> <。匹配对象;span=(274, 360), match='<reference_size>POINT2(1.6933333333333330e+01,9.7> 没有 <。匹配对象;span=(264, 320), match='POINT (10.0673926721378 277.242874603175 0)> <。匹配对象;span=(234, 257), match='30,260,1,1'> <。匹配对象;span=(327, 413), match='<reference_size>POINT2(6.3764583333333320e+01,9.7> 没有 <。匹配对象;span=(215, 245), match='POINT (210 282 0)'> <。匹配对象;span=(185, 208), match='30,260,1,1'> <。匹配对象;span=(252, 338), match='<reference_size>POINT2(2.1695833333333329e+01,4.7> 没有 <。匹配对象;span=(223, 279), match='POINT (265.562621323668 220.379682473322 0)> <。匹配对象;span=(193, 216), match='30,260,1,1'> <。匹配对象;span=(286, 372), match='<reference_size>POINT2(4.1539583333333326e+01,4.7> 没有 <。匹配对象;span=(227, 282), match='POINT (93.3154363038265 121.34042743364 0)<> <。匹配对象;span=(197, 220), match='30,260,1,1'> <。匹配对象;span=(289, 375), match='<reference_size>POINT2(2.0902083333333330e+01,9.7> 没有 <。匹配对象;span=(223, 279), match='POINT (288.982168487238 92.4947277709422 0)> <。匹配对象;span=(193, 216), match='30,260,1,1'> <。匹配对象;span=(286, 372), match='<reference_size>POINT2(4.444999999999999e+01,4.7> 没有 Нет данных для обновления 裂谷热 файла.
答: 暂无答案
评论