Sure! Below is a Python script that accomplishes the tasks you've described. The script performs the following actions:
- Counts the total number of lines in the file.
- Counts the number of lines that begin with a blank space.
- Counts the number of lines that begin with an open and closed parenthesis
().
- Counts the number of lines with parentheses that contain the number one (
1).
Python Script
# Define the path to your text file
file_path = 'input.txt'
# Initialize counters
total_lines = 0
blank_start_lines = 0
parentheses_start_lines = 0
parentheses_with_one = 0
# Import regular expressions module for pattern matching
import re
# Regular expression patterns
# Pattern to check if line starts with a space
blank_start_pattern = re.compile(r'^ ')
# Pattern to check if line starts with ()
parentheses_start_pattern = re.compile(r'^\(\)')
# Pattern to find parentheses containing the number 1
parentheses_with_one_pattern = re.compile(r'\(.*1.*\)')
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
total_lines += 1
stripped_line = line.rstrip('\n') # Remove trailing newline character
# Check if line starts with a blank space
if blank_start_pattern.match(stripped_line):
blank_start_lines += 1
# Check if line starts with ()
if parentheses_start_pattern.match(stripped_line):
parentheses_start_lines += 1
# Check if any parentheses in the line contain the number 1
if parentheses_with_one_pattern.search(stripped_line):
parentheses_with_one += 1
# Print the results
print(f"Total number of lines: {total_lines}")
print(f"Number of lines starting with a blank space: {blank_start_lines}")
print(f"Number of lines starting with () : {parentheses_start_lines}")
print(f"Number of lines with parentheses containing the number 1: {parentheses_with_one}")
except FileNotFoundError:
print(f"The file {file_path} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
Explanation of the Script
-
Importing Modules:
re: This module is used for regular expression operations, which help in pattern matching within the lines.
-
Defining the File Path:
file_path: Replace 'input.txt' with the path to your text file if it's located elsewhere or has a different name.
-
Initializing Counters:
total_lines: Counts the total number of lines in the file.
blank_start_lines: Counts lines that start with a blank space.
parentheses_start_lines: Counts lines that start with ().
parentheses_with_one: Counts lines that contain parentheses enclosing the number 1.
-
Defining Regular Expressions:
blank_start_pattern: Matches lines that start with a space.
parentheses_start_pattern: Matches lines that start with ().
parentheses_with_one_pattern: Matches any occurrence of parentheses containing the number 1 anywhere in the line.
-
Reading and Processing the File:
- The script opens the file in read mode with UTF-8 encoding.
- It iterates through each line, incrementing
total_lines.
- For each line, it removes the trailing newline character for accurate pattern matching.
- It checks the line against each pattern and increments the respective counters if there's a match.
-
Handling Exceptions:
- If the specified file does not exist, it prints an error message.
- Any other exceptions are caught and printed to help with debugging.
-
Displaying the Results:
- After processing the file, the script prints out the counts for each category.
How to Use the Script
-
Prepare Your Text File:
- Ensure you have a text file (e.g.,
input.txt) in the same directory as your script or provide the correct path to the file.
-
Run the Script:
- Save the script to a
.py file, for example, line_counter.py.
- Open a terminal or command prompt.
- Navigate to the directory containing your script.
- Run the script using the command:
python line_counter.py
- Make sure you have Python installed on your system. You can download it from python.org if you haven't already.
-
View the Output:
- The script will display the counts based on the content of your text file.
Example
Suppose your input.txt contains the following lines:
This is the first line.
(This line starts with a space.
() This line starts with parentheses.
Here is a line with (1) inside parentheses.
Another line with (something1else).
Line without special patterns.
()
Running the script would produce:
Total number of lines: 7
Number of lines starting with a blank space: 1
Number of lines starting with () : 2
Number of lines with parentheses containing the number 1: 2
Notes
-
Regular Expressions:
- The
^ symbol in regex denotes the start of a line.
\( and \) are used to match the literal parentheses characters.
.*1.* ensures that the number 1 is present somewhere within the parentheses.
-
Whitespace Handling:
- The script specifically checks for lines starting with a single space. If you want to include any whitespace character (like tabs), you can modify the
blank_start_pattern to r'^\s'.
-
Customization:
- Feel free to modify the script to suit additional requirements or to handle more complex patterns as needed.
I hope this helps! Let me know if you have any questions or need further assistance.