鱼C论坛

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

[已解决]小甲鱼31节pickle模块课后作业

[复制链接]
发表于 2017-8-24 20:33:29 | 显示全部楼层 |阅读模式

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

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

x
  1. name=input('请输入你想编辑保存的文件名:')
  2. import pickle
  3. def text_split(name):
  4.     num=1
  5.     c=[]
  6.     d=[]
  7.     f=open(name)
  8.     for i in f:
  9.         if i[0:3]=='小客服':
  10.             print(i)
  11.             f.seek(6,1)
  12.             c.append(f.readline)
  13.             
  14.             
  15.         elif i[0:3]=='小甲鱼':
  16.                 print(i)
  17.                 f.seek(6,1)
  18.                 d.append(f.readline())
  19.             
  20.         elif i[0:3]=='===':
  21.                 print(i)
  22.                 a=open('girl%d.txt'%num,'wb')
  23.                 pickle.dump(c,a)
  24.                 a.close()
  25.                 b=open('boy%d.txt'%num,'wb')
  26.                 pickle.dump(d,b)
  27.                 b.close()
  28.                 num+=1
  29.                 print(num)
  30.                
  31.     a=open('girl%d.txt'%num,'wb')
  32.     pickle.dump(c,a)
  33.     a.close()
  34.     b=open('boy%d.txt'%num,'wb')
  35.     pickle.dump(d,b)
  36.     b.close()
  37. text_split(name)
复制代码

我写的代码,运行后出现:f.seek(6,1)
io.UnsupportedOperation: can't do nonzero cur-relative seeks
为什么会出现这个错误?
最佳答案
2017-8-24 20:55:03
In text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)).


意思是对于非二进制的文本文件,不允许使用偏移定位。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-8-24 20:55:03 | 显示全部楼层    本楼为最佳答案   
In text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2)).


意思是对于非二进制的文本文件,不允许使用偏移定位。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-30 10:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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