1. Use Cases#
- Under Windows operating system
- Creating small databases, such as entry explanations
- Each line needs to be saved as a separate text
- The text name is the word or letter string at the beginning of each line
- There are no spaces or special characters between letter strings
- The text to be split is as follows, recommended to be in utf-8 encoding format
abrade v. abrasion; wear
abscess n. abscess
abstain v. abstain; avoid; abstention
2. Tools#
- Python
3. Implementation Steps#
(1) Install Python#
- Download and install from the official Python website
- Click next all the way, default settings are fine
- No need to open it after installation
(2) Copy the Code#
- Create a new text file in any location and paste the following content
# Define text content
text = """
abrade v. abrasion; wear
abscess n. abscess
abstain v. abstain; avoid; abstention
"""
# Split the text into lines
lines = text.strip().split('\n')
# Iterate through each line to create files
for line in lines:
# Extract the word as the filename
# Assume each word is the first word of each line followed by a space
filename = line.split(' ')[0]
# Create a new file and write the content
# Use with statement to ensure the file is properly closed
with open(f"{filename}.txt", 'w', encoding='utf-8') as file:
file.write(line)
# Print completion message
print("All files have been created.")
- Note to replace the demo text (three lines of words and definitions) in the above code with the actual text to be split
- Click File - Save As and select All Files for the save type
- Enter the filename create_files.py and save it to a location, such as the 123 folder on drive D
- At this point, copying the code is complete
- Additionally, if you want to generate md format text, simply change txt to md
(3) Execute the Code#
- Open the DOS command line in the Windows system
- For example, press Win+R, type CMD, and hit enter
- Or from Start - Accessories - Command Line
- Switch to drive D, then use the cd space command to enter the 123 folder and run the code
D:
cd D:\123
create_files.py
-