鱼C论坛

 找回密码
 立即注册
查看: 2515|回复: 6

[已解决]终端读出txt文件时出现乱码

[复制链接]
发表于 2020-5-19 11:09:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大神们,我写的这个代码为什么读一个txt读出来却是一堆乱码呢

  1. from sys import argv

  2. script, filename = argv

  3. txt = open(filename)

  4. print(f"Here's your file {filename}")
  5. print(txt.read())

  6. print('Type the filename again:')
  7. file_again = input('>')

  8. txt_again = open(file_again)

  9. print(txt_again.read())
复制代码



                               
登录/注册后可看大图


  1. lizehongdeMacBook-Air:15_读取文件 lizehong$ python3.8 15_读取文件.py 15_text.txtHere's your file 15_text.txt
  2. {\rtf1\ansi\ansicpg936\cocoartf1671\cocoasubrtf600
  3. {\fonttbl\f0\fnil\fcharset0 Menlo-Regular;}
  4. {\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
  5. {\*\expandedcolortbl;;\csgray\c0;}
  6. \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
  7. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0

  8. \f0\fs22 \cf2 \CocoaLigature0 Ashhhahakhakfhliaufehi\
  9. fhaoiefhiofhvkdjbv\
  10. adaiofhoiefojireofhovih}
  11. Type the filename again:
  12. >15_text.txt
  13. {\rtf1\ansi\ansicpg936\cocoartf1671\cocoasubrtf600
  14. {\fonttbl\f0\fnil\fcharset0 Menlo-Regular;}
  15. {\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
  16. {\*\expandedcolortbl;;\csgray\c0;}
  17. \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
  18. \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0

  19. \f0\fs22 \cf2 \CocoaLigature0 Ashhhahakhakfhliaufehi\
  20. fhaoiefhiofhvkdjbv\
  21. adaiofhoiefojireofhovih}
复制代码
最佳答案
2020-5-19 11:26:48

查看原始文件编码格式
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-19 11:10:58 | 显示全部楼层
试着这样打开?
  1. txt = open(filename,encoding='utf-8')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 11:10:59 | 显示全部楼层
open那里加个参数:
  1. encoding="utf-8"
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 11:13:10 | 显示全部楼层
或者
  1. encoding="gbk"
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 11:15:36 | 显示全部楼层
  1. lizehongdeMacBook-Air:15_读取文件 lizehong$ python3.8 15_读取文件.py 15_text.txtHere's your file 15_text.txt
  2. > 15_text.txt
  3. > 15_text.txt
  4. > 15_text.txt
  5. > 15_text.txt
  6. > 15_text.txt
  7. > 15_text.txt
  8. > 15_text.txt
  9. > 15_text.txt
  10. > 15_text.txt
复制代码

各位,不行啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 11:26:48 | 显示全部楼层    本楼为最佳答案   

查看原始文件编码格式
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 11:53:25 | 显示全部楼层
你打开的文件并非文本文件,而是RTF文件,RTF是RichTextFormat的缩写,意即富文本格式,这是一种类似DOC格式(Word文档)的文件。windows中可以用word或者写字板打开,MAC OS用也有相应的程序可以打开。所以作为练习,你需要换一个真正的文本文件了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 05:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表