筛选小说文本的python脚本–4个脚本
有1个文件为a.txt
1:使得a.txt文件中的所有文本按每行的首字母顺序排列,【有排序】
2:仅仅获取文件中A开头的行,【有排序】
3:仅仅获取文件中包含“Button”的行,【有排序】
4: 仅仅获取文件中不包含“Button”的行,【有排序】
# Open the file in read mode
file = open("a.txt",'r', encoding='utf-8')
# Read the file content as a string
content = file.read()
# Split the string by newline characters
lines = content.split("\n")
# Initialize an empty list to store the lines starting with A
alist = []
# Loop through the lines and check if they start with A
for line in lines:
alist.append(line)
# Close the file
file.close()
# Print the alist list
print(alist)
alist.sort()
print("列表")
for i in alist:
print(i)
# Open the file in read mode
file = open("a.txt",'r', encoding='utf-8')
# Read the file content as a string
content = file.read()
# Split the string by newline characters
lines = content.split("\n")
# Initialize an empty list to store the lines starting with A
alist = []
# Loop through the lines and check if they start with A
for line in lines:
if line.startswith("A"):
# Append the line to the alist list
alist.append(line)
# Close the file
file.close()
# Print the alist list
print(alist)
alist.sort()
print("列表")
for i in alist:
print(i)
# Open the file in read mode
file = open("a.txt", "r")
# Read the file content as a string
content = file.read()
# Split the string by newline characters
lines = content.split("\n")
# Initialize an empty list to store the lines containing B
blist = []
# Loop through the lines and check if they contain B
for line in lines:
if "Button" in line:
# Append the line to the blist list
blist.append(line)
# Close the file
file.close()
blist.sort()
# Print the blist list
print(blist)
print("列表")
for i in blist:
print(i)
# Open the file in read mode
file = open("a.txt", "r")
# Read the file content as a string
content = file.read()
# Split the string by newline characters
lines = content.split("\n")
# Initialize an empty list to store the lines containing B
blist = []
# Loop through the lines and check if they contain B
for line in lines:
if "Butt" not in line:
# Append the line to the blist list
blist.append(line)
# Close the file
file.close()
blist.sort()
# Print the blist list
print(blist)
print("列表")
for i in blist:
print(i)