【问题标题】:How do I remove the first few characters of a file with python?如何使用 python 删除文件的前几个字符?
【发布时间】:2015-11-14 15:11:43
【问题描述】:

我有几个日志文件,每个都有超过 100 万行。 我不想删除每个文件的前三行以及第四行的前 9 个字符。

我可以删除前 3 行,但是,我无法弄清楚如何删除第四行的前 9 个字符并保留文档的其余部分。

样本数据:

#Software: Microsoft Internet Information Services 7.5
#Version: 1.0
#Date: 2015-06-02 00:00:00
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-  username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken

期望的输出:

date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken

到目前为止我的代码:

for filename in os.listdir(path):
    basename, ext = os.path.splitext(filename)
    fullname = os.path.join(path, filename)
    newname = os.path.join(path, basename + '-out' + ext)
    with open(fullname) as read:
        #skip first 3 lines
        for n in xrange(3):
            read.readline()
        # hand the rest to shutil.copyfileobj
        with open(newname, 'w') as write:
            shutil.copyfileobj(read, write)

【问题讨论】:

  • 阅读3行后尝试添加read.read(9)
  • 删除那些字节,或者你能用空格替换它们吗?我问是因为覆盖这些字节然后重写整个文件会更有效(如果你不想在开头有那个空格,你必须这样做)。
  • 我确实需要删除它们,因为脚本的其余部分会将文件转换为 csv 文件。

标签: python python-2.7 slice


【解决方案1】:

你很亲密:

for filename in os.listdir(path):
    basename, ext = os.path.splitext(filename)
    fullname = os.path.join(path, filename)
    newname = os.path.join(path, basename + '-out' + ext)
    with open(fullname) as read:
        #skip first 3 lines
        for n in xrange(3):
            read.readline()
        # consume 9 bytes    <<<<<< ADDED THIS <<<<<
        read.read(9)  #      <<<<<< ADDED THIS <<<<<
        # hand the rest to shutil.copyfileobj
        with open(newname, 'w') as write:
            shutil.copyfileobj(read, write)

【讨论】:

    【解决方案2】:

    您已经完成了 99% 的工作。剩下的就是在复制前将读指针前移 9 个字符。

        #skip first 3 lines
        for n in xrange(3):
            read.readline()
        # Skip 9 characters
        read.read(9)
        # hand the rest to shutil.copyfileobj
        with open(newname, 'w') as write:
            shutil.copyfileobj(read, write)
    

    【讨论】:

      【解决方案3】:

      感谢您提供的信息...虽然我无法使用 read.read() 选项来处理有关向前移动读取指针的评论指出我的方向是正确的。

      我只是选择了将指针位置前移 108,然后读取文件。

      最终有效的代码:

      for filename in os.listdir(path):
          basename, ext = os.path.splitext(filename)
          fullname = os.path.join(path, filename)
          newname = os.path.join(path, basename + '-out' + ext)
          with open(fullname) as read:
              #skip first two lines
              read.seek(108)
              for n in xrange(0):            
                  read.readline()
              # hand the rest to shutil.copyfileobj
              with open(newname, 'w') as write:
                  shutil.copyfileobj(read, write)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-01
        • 2021-01-21
        • 2011-11-14
        • 1970-01-01
        相关资源
        最近更新 更多