input输入多行文本,保存为.dot文件和对应的.txt文件

需求

复制代码
不管是上面的dot还是这个dot
变成
input输入文本
 按"# ꧂ ꧁"结束
 保存在dot文本文件夹下,

用txt保存每个文件文件名:
编号. 第二行有字文字  时间戳 

代码

首先,我会创建一个Python脚本,它将接受用户的输入,直到输入以# ꧂ ꧁结束,然后将输入保存为.dot文件和对应的.txt文件。代码如下:

python 复制代码
import os
from datetime import datetime

def save_input_to_files():
    input_text = ""
    file_number = 1

    while True:
        line = input().strip()
        if line == "# ꧂ ꧁":
            break
        input_text += line + "\n"

    # 生成时间戳
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    # 写入到dot文件中
    dot_filename = f"{file_number}.dot"
    with open(dot_filename, "w") as dot_file:
        dot_file.write(input_text)

    # 写入到txt文件中
    txt_filename = f"{file_number}.txt"
    with open(txt_filename, "w") as txt_file:
        txt_file.write(f"File Number: {file_number}\n")
        txt_file.write(f"Timestamp: {timestamp}\n")
        txt_file.write(input_text)

    print(f"Input saved to {dot_filename} and {txt_filename}")

    file_number += 1

if __name__ == "__main__":
    save_input_to_files()
相关推荐
ZhengEnCi1 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187912 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L19 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅19 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅19 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L19 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅19 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L20 小时前
python的类&继承
python
Warson_L20 小时前
类型标注/type annotation
python