鱼C论坛

 找回密码
 立即注册
查看: 1398|回复: 3

[已解决]31讲 不明白的循环体

[复制链接]
发表于 2018-3-25 09:26:53 | 显示全部楼层 |阅读模式

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

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

x
  1. import pickle
  2. def save_file(boy, girl, count):
  3.     file_name_boy = 'boy_' + str(count) + '.txt'
  4.     file_name_girl = 'girl_' + str(count) + '.txt'

  5.     boy_file = open(file_name_boy, 'wb') # 记得一定要加 b 吖
  6.     girl_file = open(file_name_girl, 'wb') # 记得一定要加 b 吖

  7.     pickle.dump(boy, boy_file)
  8.     pickle.dump(girl, girl_file)

  9.     boy_file.close()
  10.     girl_file.close()

  11. def split_file(file_name):
  12.     count = 1
  13.     boy = []
  14.     girl = []

  15.     f = open(file_name)

  16.     for each_line in f:
  17.         if each_line[:6] != '======':
  18.             (role, line_spoken) = each_line.split(':', 1)
  19.             if role == '小甲鱼':
  20.                 boy.append(line_spoken)
  21.             if role == '小客服':
  22.                 girl.append(line_spoken)
  23.         else:
  24.             save_file(boy, girl, count)

  25.             boy = []
  26.             girl = []
  27.             count += 1

  28.     save_file(boy, girl, count)
  29.     f.close()

  30. split_file('record.txt')
复制代码

代码以上,不明白 for each_line in f:        if each_line[:6] != '======':     这里的each_line  是一行的字符吗?  为什么啊  
不应该是一个字符吗?
最佳答案
2018-3-25 09:59:50
就是每一行,如果直接指向每一个字符,你想想那个处理操作简单
书上视频都有,这就是规定
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-25 09:59:50 | 显示全部楼层    本楼为最佳答案   
就是每一行,如果直接指向每一个字符,你想想那个处理操作简单
书上视频都有,这就是规定
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-25 15:45:16 | 显示全部楼层

"Interactive" text files (files for which isatty() returns True)
      use line buffering.  Other text files use the policy described above
      for binary files.
newline controls how universal newlines works (it only applies to text
    mode). It can be None, '', '\n', '\r', and '\r\n'.  It works as
    follows:
   
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
      these are translated into '\n' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
   
    * On output, if newline is None, any '\n' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '\n', no translation takes place. If newline is any
      of the other legal values, any '\n' characters written are translated
      to the given string.
    查了好久,终于感觉明白了。
交互式文件使用行进行缓冲,即每行作为一个可迭代对象的元素。
使用open打开文件时,换行符=none,其=\n的作用,使用for循环,首先将其整个文件放入迭代器,然后遍历时以行为一个单位。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-25 16:37:35 | 显示全部楼层
塔利班 发表于 2018-3-25 09:59
就是每一行,如果直接指向每一个字符,你想想那个处理操作简单
书上视频都有,这就是规定

谢谢,查到了规定
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-9 18:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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