file = open("something.py","r")
infile = file.readlines()
for line in infile:
if line.find("#") !=-1:
index = line.find("#")
print("BLABLABLA",the line that begins with #,"BLABLABLA", end="")
# Open file for reading
with open("infile.txt", "r") as infile:
# Open another file for writing
with open("outfile.txt", "w") as outfile:
# Read each line from the input file
for line in infile.readlines():
# Check if line begins with keyword "Hello"
if line.startswith("Hello"):
# If it does, prepend and append something to the line
line = "Prepend! >>> " + line.strip() + " <<< Append!"
# Finally, write the line to outfile
outfile.write(line + "\n")
输入:
Hello, this is a line.
Hello, this is another line.
Goodbye, these were all the lines
输出:
Prepend! >>> Hello, this is a line. <<< Append!
Prepend! >>> Hello, this is another line. <<< Append!
Goodbye, these were all the lines
简单(&A);很短。最后两行将根据您的标志和您想对读取的行实际执行的操作而改变
这个怎么样:
输入:
输出:
相关问题 更多 >
编程相关推荐