鱼C论坛

 找回密码
 立即注册
查看: 1196|回复: 1

[已解决]课后作业29题,求助大佬

[复制链接]
发表于 2021-7-27 21:29:52 | 显示全部楼层 |阅读模式

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

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

x
def file_reader(name,num):
      list1 = num.split(sep = ':')
      began = list1[0]
      last = list1[1]
      f1 = open(name)
      if began == ' ':
            if last ==' ':
                  print('文件%s的全文内容如下:' % name)
                  print(f1.read())
            else:
                  print('文件%s从开头到第%s行的内容如下:')
                  lines = int(last)
                  while lines:
                        print(f1.readline())
                        lines -= 1
      elif began != ' ':
            time1 = int(began)
            time1.1 = time1
            while time1 != 1:
                  f1.readline()
                  time1 -= 1
            if last == ' ':
                  print('文件%s从第%s行到末尾的内容如下:' % (name,began))
                  print(f1.read())
            else:
                  time2 = int(last)
                  time3 = time2 - time1.1 + 1
                  print('文件%s从第%s行到第%s行的内容下所示:' % (name,began,last))
                  while time3:
                        print(f1.readline())
                        time3 -= 1
      f1.close()

name = input('请输入需要打开的文件:')
num = input('请输入需要显示的行数:')
file_reader(name,num)



会报错
Traceback (most recent call last):
  File "D:\phython\课后作业29 3.py", line 36, in <module>
    file_reader(name,num)
  File "D:\phython\课后作业29 3.py", line 20, in file_reader
    f1.readline()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 26: illegal multibyte sequence
最佳答案
2021-7-27 21:34:37
解码问题,试一下给open函数加上encoding = 'utf-8'参数

  1. def file_reader(name,num):
  2.       list1 = num.split(sep = ':')
  3.       began = list1[0]
  4.       last = list1[1]
  5.       f1 = open(name,'r',encoding = 'utf-8')
  6.       if began == ' ':
  7.             if last ==' ':
  8.                   print('文件%s的全文内容如下:' % name)
  9.                   print(f1.read())
  10.             else:
  11.                   print('文件%s从开头到第%s行的内容如下:')
  12.                   lines = int(last)
  13.                   while lines:
  14.                         print(f1.readline())
  15.                         lines -= 1
  16.       elif began != ' ':
  17.             time1 = int(began)
  18.             time1_1 = time1
  19.             while time1 != 1:
  20.                   f1.readline()
  21.                   time1 -= 1
  22.             if last == ' ':
  23.                   print('文件%s从第%s行到末尾的内容如下:' % (name,began))
  24.                   print(f1.read())
  25.             else:
  26.                   time2 = int(last)
  27.                   time3 = time2 - time1_1 + 1
  28.                   print('文件%s从第%s行到第%s行的内容下所示:' % (name,began,last))
  29.                   while time3:
  30.                         print(f1.readline())
  31.                         time3 -= 1
  32.       f1.close()

  33. name = input('请输入需要打开的文件:')
  34. num = input('请输入需要显示的行数:')
  35. file_reader(name,num)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-27 21:34:37 | 显示全部楼层    本楼为最佳答案   
解码问题,试一下给open函数加上encoding = 'utf-8'参数

  1. def file_reader(name,num):
  2.       list1 = num.split(sep = ':')
  3.       began = list1[0]
  4.       last = list1[1]
  5.       f1 = open(name,'r',encoding = 'utf-8')
  6.       if began == ' ':
  7.             if last ==' ':
  8.                   print('文件%s的全文内容如下:' % name)
  9.                   print(f1.read())
  10.             else:
  11.                   print('文件%s从开头到第%s行的内容如下:')
  12.                   lines = int(last)
  13.                   while lines:
  14.                         print(f1.readline())
  15.                         lines -= 1
  16.       elif began != ' ':
  17.             time1 = int(began)
  18.             time1_1 = time1
  19.             while time1 != 1:
  20.                   f1.readline()
  21.                   time1 -= 1
  22.             if last == ' ':
  23.                   print('文件%s从第%s行到末尾的内容如下:' % (name,began))
  24.                   print(f1.read())
  25.             else:
  26.                   time2 = int(last)
  27.                   time3 = time2 - time1_1 + 1
  28.                   print('文件%s从第%s行到第%s行的内容下所示:' % (name,began,last))
  29.                   while time3:
  30.                         print(f1.readline())
  31.                         time3 -= 1
  32.       f1.close()

  33. name = input('请输入需要打开的文件:')
  34. num = input('请输入需要显示的行数:')
  35. file_reader(name,num)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 04:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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