繁体   English   中英

如何使用python删除文件的前几个字符?

[英]How do I remove the first few characters of a file with python?

我有几个日志文件,每个文件最多都有一百万行。 我不会删除每个文件的前三行以及第四行的前9个字符。

我可以删除前三行,但是,我仍然无法弄清楚如何删除第四行的前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)

您非常接近:

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)

您已经完成了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)

感谢您提供的信息...尽管我无法使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM