I am working on creating a python file to translate a path found within an svg file, translating it into a csv file, however I am restricted from using any libraries other than csv. I have gotten this far and it will not export any data into the csv file. I am wondering where I have gone wrong.
import csv
def parse_coordinates(svg_path):
commands = set(['M', 'm', 'L', 'l', 'H', 'h', 'V', 'v'])
current_x, current_y = None, None
coordinates = []
parts = svg_path.split()
for part in parts:
print("PartL", part)
print(part)
if part[0] in commands:
command = part
else:
coords = list(map(float, part.split(',')))
if command == 'M' or command == 'm':
current_x, current_y = coords[0], coords[1]
coordinates.append([current_x, current_y])
elif command == 'L' or command == 'l':
current_x, current_y = coords[0], coords[1]
coordinates.append([current_x, current_y])
elif command == 'H' or command == 'h':
current_x = coords[0]
coordinates.append([current_x, current_y])
elif command == 'V' or command == 'v':
current_y = coords[0]
coordinates.append([current_x, current_y])
return coordinates
# Read SVG file
with open('Path5.svg', 'r') as f:
svg_content = f.read()
# Find the path element by parsing the content directly
start_idx = svg_content.find(' d="') + 3
end_idx = svg_content.find('"', start_idx)
path_data = svg_content[start_idx:end_idx]
# Parse coordinates from the path data
coordinates = parse_coordinates(path_data)
# Write coordinates to CSV file
with open("coordinates.csv", "a", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(coordinates)
print("Coordinates written to coordinates.csv")
Here is the svg I am working off of:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="759.3833mm"
height="460.74869mm"
viewBox="0 0 759.3833 460.74869"
version="1.1"
id="svg1"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs1" />
<g
id="layer1"
transform="translate(-57.875793,-38.539758)">
<path
style="fill:none;stroke:#000000;stroke-width:0.264583"
d="M 58.008075,50.130434 63.021117,499.1559 817.12609,497.7236 816.40994,364.51987 H 150.3913 V 152.53975 H 805.6677 V 38.67205 H 148.24285"
id="path1" />
</g>
</svg>
I have tried printing out the coordinates to see what I am receiving and that is returning blank. I have tried a different style of data gathering such as:
import csv
def parse_coordinates(svg_path):
coordinates = []
# Split the path data by spaces
parts = svg_path.split()
# Initialize variables to track the current command and coordinates
current_x, current_y = None, None
# Iterate over the parts
for part in parts:
# Check if the part starts with a letter (indicating a command)
if part[0].isalpha():
# Update the current command
command = part
else:
# If the part is not a command, it must be coordinates
# Split the part by commas to extract x and y coordinates
x_y = part.split(',')
if len(x_y) == 2:
x, y = map(float, x_y)
current_x, current_y = x, y
coordinates.append((x, y))
elif command == "H":
x = float(x_y[0])
current_x = x
coordinates.append((x, current_y))
elif command == "V":
y = float(x_y[0])
current_y = y
coordinates.append((current_x, y))
return coordinates
# Read SVG file
with open('Path1.svg', 'r') as f:
svg_content = f.read()
# Find the path element by searching for '<path d="' and '"/>'
start_idx = svg_content.find('<path d="') + len('<path d="')
end_idx = svg_content.find('"', start_idx)
path_data = svg_content[start_idx:end_idx]
# Parse coordinates from the path data
coordinates = parse_coordinates(path_data)
# Write coordinates to CSV file
with open("coordinates.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
for coordinate in coordinates:
writer.writerow(coordinate)
print("Coordinates written to coordinates.csv")
However I am getting the same issue of no coordinates.