找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3039|回复: 7

[已解决] open IO 上的一个小问题

8

主题

31

帖子

31

积分

贫民

积分
31
carlosgu 发表于 2017-12-5 16:25:25 | 显示全部楼层 |阅读模式
请教一个问题,我打开任一个txt, 如果前面对file做了操作,后面的for 就不会跑下去了。但是打印出来的file 都是一样的。这是为什么?
打印结果:
C:\Anaconda3\envs\labgui\python.exe "C:/Users/ptd/Rave Lab/script/project/ruma_data_read/original_read.py"
<_io.TextIOWrapper name='tt.txt' mode='r' encoding='cp1252'>
12 52 36
52 63
<_io.TextIOWrapper name='tt.txt' mode='r' encoding='cp1252'>


code:

original_data='tt.txt'
file=open(original_data,'r')
print(file)
print(file.read())
print(file)
for line in file:
    print(len(line.split()))
回复

使用道具 举报

8

主题

31

帖子

31

积分

贫民

积分
31
carlosgu  楼主| 发表于 2017-12-5 16:42:11 | 显示全部楼层
想起来了,好像是跑过read后 就会指向file的最后。
请教:有办法不重load TXT文件,file回到开始吗?
回复 支持 反对

使用道具 举报

0

主题

956

帖子

956

积分

圣骑士

积分
956
sheeboard 发表于 2017-12-5 20:57:08 | 显示全部楼层
stackoverflow上看来的。

文件读写

文件读写
回复 支持 反对

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2017-12-6 08:27:43 | 显示全部楼层
carlosgu 发表于 2017-12-5 16:42
想起来了,好像是跑过read后 就会指向file的最后。
请教:有办法不重load TXT文件,file回到开始吗? ...

file.seek(0)回到文件起点
help(file)可以从中找到具体说明
seek(self, cookie, whence=0, /)
    Change stream position.

    Change the stream position to the given byte offset. The offset is
    interpreted relative to the position indicated by whence.  Values
    for whence are:

    * 0 -- start of stream (the default); offset should be zero or positive
    * 1 -- current stream position; offset may be negative
    * 2 -- end of stream; offset is usually negative

    Return the new absolute position.
回复 支持 反对

使用道具 举报

8

主题

31

帖子

31

积分

贫民

积分
31
carlosgu  楼主| 发表于 2017-12-6 13:13:35 | 显示全部楼层
sheeboard 发表于 2017-12-5 20:57
stackoverflow上看来的。

谢谢!
回复 支持 反对

使用道具 举报

8

主题

31

帖子

31

积分

贫民

积分
31
carlosgu  楼主| 发表于 2017-12-6 13:15:50 | 显示全部楼层
剑心无痕 发表于 2017-12-6 08:27
file.seek(0)回到文件起点
help(file)可以从中找到具体说明
seek(self, cookie, whence=0, /)

谢谢版主!
再请教下,help(file)确实出现了可以的操作,里面有seek,但是却没有详细的解释,如下所示。

class TextIOWrapper(_TextIOBase)
|  Character and line based layer over a BufferedIOBase object, buffer.
|  
|  encoding gives the name of the encoding that the stream will be
|  decoded or encoded with. It defaults to locale.getpreferredencoding(False).
|  
|  errors determines the strictness of encoding and decoding (see
|  help(codecs.Codec) or the documentation for codecs.register) and
|  defaults to "strict".
|  
|  newline controls how line endings are handled. 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.
|  
|  If line_buffering is True, a call to flush is implied when a call to
|  write contains a newline character.
|  
|  Method resolution order:
|      TextIOWrapper
|      _TextIOBase
|      _IOBase
|      builtins.object
|  
|  Methods defined here:
|  
|  __getstate__(...)
|  
|  __init__(self, /, *args, **kwargs)
|      Initialize self.  See help(type(self)) for accurate signature.
|  
|  __new__(*args, **kwargs) from builtins.type
|      Create and return a new object.  See help(type) for accurate signature.
|  
|  __next__(self, /)
|      Implement next(self).
|  
|  __repr__(self, /)
|      Return repr(self).
|  
|  close(...)
|  
|  detach(...)
|  
|  fileno(...)
|  
|  flush(...)
|  
|  isatty(...)
|  
|  read(...)
|  
|  readable(...)
|  
|  readline(...)
|  
|  seek(...)
|  
|  seekable(...)
|  
|  tell(...)
|  
|  truncate(...)
|  
|  writable(...)
|  
|  write(...)
回复 支持 反对

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2017-12-6 14:54:36 | 显示全部楼层
carlosgu 发表于 2017-12-6 13:15
谢谢版主!
再请教下,help(file)确实出现了可以的操作,里面有seek,但是却没有详细的解释,如下所示。
...

可能是编辑器的问题,没有全部打印出来(但是打印出seek(...)),可以直help(file.seek)
回复 支持 反对

使用道具 举报

8

主题

31

帖子

31

积分

贫民

积分
31
carlosgu  楼主| 发表于 2017-12-11 23:09:42 | 显示全部楼层
剑心无痕 发表于 2017-12-6 14:54
可能是编辑器的问题,没有全部打印出来(但是打印出seek(...)),可以直help(file.seek) ...

非常感谢!
pycharm无法完全显示,idle可以。
回复 支持 反对

使用道具 举报

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

本版积分规则

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