2010-01-03 31 views
8

如何检查Python中的EOF?我在代码中发现了一个错误,其中分隔符后的最后一个文本块没有添加到返回列表中。或者,也许有更好的方式来表达这个功能?如何检查Python中的EOF?

这里是我的代码:

def get_text_blocks(filename): 
    text_blocks = [] 
    text_block = StringIO.StringIO() 
    with open(filename, 'r') as f: 
     for line in f: 
      text_block.write(line) 
      print line 
      if line.startswith('-- -'): 
       text_blocks.append(text_block.getvalue()) 
       text_block.close() 
       text_block = StringIO.StringIO() 
    return text_blocks 
+0

现在尝试,固定我的功能X2 =( – 2010-01-03 04:37:18

回答

3

您可能会发现更容易使用这个解决itertools.groupby

def get_text_blocks(filename): 
    import itertools 
    with open(filename,'r') as f: 
     groups = itertools.groupby(f, lambda line:line.startswith('-- -')) 
     return [''.join(lines) for is_separator, lines in groups if not is_separator] 

另一种替代方法是使用regular expression匹配隔板:

def get_text_blocks(filename): 
    import re 
    seperator = re.compile('^-- -.*', re.M) 
    with open(filename,'r') as f: 
     return re.split(seperator, f.read()) 
+0

有趣的答案马克。我不知道itertools,谢谢。 – ajushi 2010-01-03 04:31:19

+0

+1对于RegEx版本,itertools版本有点神秘。 – 2010-01-03 04:40:00

+0

我在ineractive解释器上尝试了itertools版本,它返回一个空字符串。行似乎是一个itertools._grouper对象 – ajushi 2010-01-03 04:44:16

1

的档案结尾条件只要for语句终止持有 - 似乎minorly解决这个代码最简单的方法(你可以在年底提取text_block.getvalue()如果你想在追加它之前检查它不是空的)。

+0

感谢亚历克斯我肮脏的解决方案是增加text_blocks 。.append(text_block.getvalue())及以下的块text_block.close()它的工作原理,但它不是干:/ – ajushi 2010-01-03 04:47:17

0

为什么你需要StringIO的吗?

def get_text_blocks(filename): 
    text_blocks = [""] 
    with open(filename, 'r') as f: 
     for line in f: 
      if line.startswith('-- -'): 
       text_blocks.append(line) 
      else: text_blocks[-1] += line   
    return text_blocks 

编辑:修正了功能,其他建议可能会更好,只是想写一个类似于原来的功能。

编辑:“ - - ”假定文件开头,加入空字符串到列表中,你可以“修复” IndexError或者你可以用这一个:

def get_text_blocks(filename): 
    text_blocks = [] 
    with open(filename, 'r') as f: 
     for line in f: 
      if line.startswith('-- -'): 
       text_blocks.append(line) 
      else: 
       if len(text_blocks) != 0: 
        text_blocks[-1] += line   
    return text_blocks 

但两个版本看起来有点丑陋的我,reg-ex版本更加清洁。

+0

那还惦记着最后一块 – 2010-01-03 03:59:54

+0

能否请您提供测试输入数据 – 2010-01-03 04:04:40

+0

@maiku测试?输入数据是phpMyAdmin的SQL转储,我需要分隔blo中的文本cks用一个以 - - ...开头的行分开 – ajushi 2010-01-03 04:08:35

0

这是标准的问题与发射缓冲器。

你不检测EOF - 这是不必要的。你写最后的缓冲区。

def get_text_blocks(filename): 
    text_blocks = [] 
    text_block = StringIO.StringIO() 
    with open(filename, 'r') as f: 
     for line in f: 
      text_block.write(line) 
      print line 
      if line.startswith('-- -'): 
       text_blocks.append(text_block.getvalue()) 
       text_block.close() 
       text_block = StringIO.StringIO() 
     ### At this moment, you are at EOF 
     if len(text_block) > 0: 
      text_blocks.append(text_block.getvalue()) 
     ### Now your final block (if any) is appended. 
    return text_blocks 
1
def get_text_blocks(filename): 
    text_blocks = [] 
    text_block = StringIO.StringIO() 
    with open(filename, 'r') as f: 
     for line in f: 
      text_block.write(line) 
      print line 
      if line.startswith('-- -'): 
       text_blocks.append(text_block.getvalue()) 
       text_block.close() 
       text_block = StringIO.StringIO() 
     ### At this moment, you are at EOF 
     if len(text_block) > 0: 
      text_blocks.append(text_block.getvalue()) 
     ### Now your final block (if any) is appended. 
    return text_blocks 
-2

这是一个快速的方法,如果你有一个空文件:

if f.read(1) == '': 
print "EOF" 
f.close() 
+0

不,因为''之间没有空格。我用一个空格对文件进行了测试,但没有检测到文件是空的。 – AndroidDebaser 2013-04-23 18:46:53

+1

如果文件包含一个空格,它不是空的。 – Dave 2014-07-04 01:30:32