Skip to content

Commit 9656a28

Browse files
committed
docs(notes): add 2025-04-09-xml-regex-en.md
1 parent fab624e commit 9656a28

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Diff for: notes/2025-04-09-xml-regex-en.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: An XML Regex Script
3+
lang: en
4+
layout: post
5+
audio: false
6+
translated: false
7+
generated: true
8+
---
9+
10+
I'll modify the script to use `argparse` for handling command-line arguments. This will allow you to pass the file path directly when running the script:
11+
12+
```python
13+
import xml.etree.ElementTree as ET
14+
import re
15+
import argparse
16+
17+
def extract_look_tags(xml_file):
18+
# Read the XML file content
19+
with open(xml_file, 'r') as file:
20+
xml_content = file.read()
21+
22+
# Find all <c:look> tags using regex
23+
pattern = r'<c:look>(\d+)</c:look>'
24+
matches = re.findall(pattern, xml_content)
25+
26+
# Print the numbers
27+
for number in matches:
28+
print(number)
29+
30+
def main():
31+
# Set up argument parser
32+
parser = argparse.ArgumentParser(description='Extract numbers from <c:look> tags in an XML file')
33+
parser.add_argument('file_path', help='Path to the XML file')
34+
35+
# Parse arguments
36+
args = parser.parse_args()
37+
38+
# Process the file
39+
extract_look_tags(args.file_path)
40+
41+
if __name__ == "__main__":
42+
main()
43+
44+
```
45+
46+
Now you can run the script from the command line like this:
47+
48+
```
49+
python script_name.py path/to/your/xml_file.xml
50+
```
51+
52+
The script will:
53+
1. Use `argparse` to handle the command-line argument for the file path
54+
2. Parse the XML file to find all `<c:look>` tags
55+
3. Extract and print the numbers inside these tags
56+
57+
This approach makes the script more user-friendly and suitable for scripting or batch processing.

0 commit comments

Comments
 (0)