打印print的另一种方式sys.stdout

介绍了tmp的作用,它可保存当前的sys.stdout,在修改sys.stdout后能找回之前的,使print输出到屏幕。还提及若想用print将打印内容导出到文件,会在相应目录生成data.txt文件。
>>> import sys
>>> tmp = sys.stdout
>>> sys.stdout=open('log.txt','a')
>>> print('cisco')
>>> print(1,2,3)
>>> sys.stdout.close()
>>> sys.stdout=tmp
>>> print('haha')
haha
>>> print(open('log.txt','rb').read())
b'welcome to haha\r\ncisco\r\n1 2 3\r\n'
>>> print(open('log.txt','r').read())
welcome to haha
cisco
1 2 3

>>> 

  tmp作用是保存当前的sys.stdout,修改了sys.stdout后能找回之前的sys.stdout,来让print能输出到屏幕

如果想用print实现把打印的内容导出到文件:

>>> x,y,z='haha',88,['python','ericsson']
>>> x
'haha'
>>> y
88
>>> z
['python', 'ericsson']
>>> print(x,y,z,sep='...!', file=open('data.txt','w'))
>>> print(x,y,z)
haha 88 ['python', 'ericsson']
>>> file.close()
>>> print(open('data.txt','r').read())
haha...!88...!['python', 'ericsson']

  在该目录下,会生成一个data.txt文件

 

转载于:https://www.cnblogs.com/vigossr/p/11176008.html

#!/usr/bin/env python3 import os import sys import tty import termios import requests import json # 配置参数(建议通过环境变量设置 API_KEY) API_KEY = os.environ.get("DEEPSEEK_API_KEY", "sk-f4e5e260d48f4d22b71dd991de75d31b") MODEL = "deepseek-chat" API_URL = "https://api.deepseek.com/v1/chat/completions" HISTORY_FILE = os.path.expanduser("~/.deepseek_history.json") END_SEQUENCE = ";;" # 自定义结束序列 class TerminalInput: """处理终端多行输入和特殊快捷键的类""" def __init__(self): self.fd = sys.stdin.fileno() self.old_settings = termios.tcgetattr(self.fd) def __enter__(self): """进入原始输入模式""" tty.setraw(self.fd) return self def __exit__(self, exc_type, exc_value, traceback): """恢复终端原始设置""" termios.tcsetattr(self.fd, termios.TCSADRAIN, self.old_settings) def get_multiline_input(self, prompt): """获取多行用户输入,支持自定义结束序列""" sys.stdout.write(prompt) sys.stdout.flush() input_buffer = [] last_char = None line_count = 0 while True: ch = sys.stdin.read(1) # 检测结束序列 if ch == END_SEQUENCE[0] and last_char == END_SEQUENCE[1]: # 删除结束序列的第一个字符 if input_buffer and input_buffer[-1] == END_SEQUENCE[1]: input_buffer.pop() sys.stdout.write("\b \b") # 清除上一个字符 return ''.join(input_buffer).rstrip(END_SEQUENCE) # 处理回车键 - 添加换行符 if ch == '\r' or ch == '\n': input_buffer.append('\n') sys.stdout.write('\n') line_count += 1 # 显示继续输入提示 if line_count > 0: sys.stdout.write("... ") # 多行输入提示符 sys.stdout.flush() last_char = ch continue # 处理退格键 if ch == '\x7f': # Backspace if input_buffer: # 特殊处理:删除换行符 if input_buffer[-1] == '\n': # 删除换行符并移动到上一行 input_buffer.pop() sys.stdout.write('\033[1A') # 移动到上一行 sys.stdout.write('\033[K') # 清除行 line_count -= 1 else: # 删除普通字符 input_buffer.pop() sys.stdout.write('\b \b') # 清除上一个字符 sys.stdout.flush() last_char = ch continue # 处理普通字符 input_buffer.append(ch) sys.stdout.write(ch) sys.stdout.flush() last_char = ch def main(): # 检查API密钥 if API_KEY == "your_api_key_here": print("请先设置API密钥:") print("方法1: 在脚本中修改API_KEY") print("方法2: 执行命令: export DEEPSEEK_API_KEY='your_key'") return # 加载对话历史 messages = load_history() print(f"\n DeepSeek 终端助手已启动 (输入 '{END_SEQUENCE}' 结束多行输入并发送,输入 'exit' 退出)") with TerminalInput() as term_input: while True: try: # 获取用户输入 user_input = term_input.get_multiline_input("\n 你: ") # 退出处理 if user_input.strip().lower() == "exit": save_history(messages) print("\n对话历史已保存至", HISTORY_FILE) break # 空输入处理 if not user_input.strip(): continue # 添加用户消息 messages.append({"role": "user", "content": user_input}) # 打印AI响应提示 print("\n AI: ", end="", flush=True) # 获取并显示AI响应 full_response = "" for chunk in stream_response(messages): if chunk: print(chunk, end="", flush=True) full_response += chunk print() # 确保换行 # 保存上下文 messages.append({"role": "assistant", "content": full_response}) if len(messages) > 6: # 保留最近3轮对话 messages = messages[-6:] except KeyboardInterrupt: print("\n已中断") save_history(messages) break except Exception as e: print(f"\n错误: {str(e)}") def stream_response(messages): """流式请求核心逻辑""" headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } data = { "model": MODEL, "messages": messages, "stream": True, "temperature": 0.7 } try: with requests.post(API_URL, headers=headers, json=data, stream=True, timeout=30) as res: res.raise_for_status() for line in res.iter_lines(): if line: decoded = line.decode('utf-8') if decoded.startswith("data:"): try: chunk = json.loads(decoded[5:]) if content := chunk.get("choices", [{}])[0].get("delta", {}).get("content"): yield content except json.JSONDecodeError: continue except requests.exceptions.RequestException as e: raise ConnectionError(f"API请求失败: {str(e)}") def load_history(): """加载历史对话""" if os.path.exists(HISTORY_FILE): try: with open(HISTORY_FILE, 'r') as f: return json.load(f) except: return [] return [] def save_history(messages): """保存对话历史""" with open(HISTORY_FILE, 'w') as f: json.dump(messages, f, ensure_ascii=False, indent=2) if __name__ == "__main__": main() 这个换行很乱导致返回格式看不懂 帮忙解决一下
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值