testxlog

testxlog

Split the file by lines and name it with the word at the beginning of each line.

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 Used#

  • 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 the 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 demonstration text (three lines of words and definitions) in the code above 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 the D drive
  • At this point, copying the code is complete
  • Additionally, 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 go to Start - Accessories - Command Line
  • Switch to the D drive and use the cd space command to enter the 123 folder and run the code
D:
cd D:\123
create_files.py

-photo by Juan Antonio Guzmán(https://unsplash.com/@juanaan95?utm_source=templater_proxy&utm_medium=referral&fit=crop&mask=corners) on Unsplash

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.