正在删除Big CSV文件的第一行?

时间:2015-01-13 08:28:15

标签: python csv python-3.x bigdata

如何在python中删除大型CSV文件的第一行? 我在这里查看了以前的解决方案,其中一个是:

with open("test.csv",'r') as f:
    with open("updated_test.csv",'w') as f1:
        f.next() # skip header line
        for line in f:
            f1.write(line)

给了我这个错误:

f.next() # skip header line
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'

另一个解决方案是:

with open('file.txt', 'r') as fin:
    data = fin.read().splitlines(True)
with open('file.txt', 'w') as fout:
    fout.writelines(data[1:])

这带来了记忆问题!

3 个答案:

答案 0 :(得分:10)

f.next()替换为next(f)

with open("test.csv",'r') as f, open("updated_test.csv",'w') as f1:
    next(f) # skip header line
    for line in f:
        f1.write(line)

答案 1 :(得分:0)

使用 f .__ next __()而不是f.next()

文档在这里: https://docs.python.org/3/library/stdtypes.html#iterator.next

答案 2 :(得分:0)

使用<div class="modal-body"> <h5 style="font-family: Century Gothic">SEARCH ID NO. <form method="Post"> <div class="form-group form-md-line-input has-danger"> <div class="input-icon"> <input type="text" class="form-control" name="Search_ID" style="padding-left: 5px" autocomplete="off"> <label style="font-weight: bolder; font-size: 11px; font-family: Arial Narrow ">INSERT ID NUMBER OF THE EMPLOYEE YOU WISH TO UPDATE.</label> <span class="help-block" style="font-size: 10px; text-align: right;">Update Form will show when successfully entered ID No.</span> <!--<i class="fa fa-info"></i>--> </div> </div> </form> </h5> <?php if (!isset($_POST['Search_ID'])) { ?> <div class="row" id="Hide_Div"> <br/> <div class="col-md-4"> <div class="form-group form-md-line-input has-danger"> <div class="input-icon"> <input type="text" class="form-control" name="id_no" style="padding-left: 10px"> <label style="font-weight: bolder; font-size: 11px; font-family: Arial Narrow ">EMPLOYEE ID NO.</label> <span class="help-block" style="font-size: 10px">Insert Employee ID No...</span> </div> </div> </div> <div class="col-md-8"> <div class="form-group"> <label style="font-weight: bolder; font-size: 11px; font-family: Arial Narrow; color: grey">EMPLOYEE STATUS</label> <div class="kt-radio-inline"> <label class="kt-radio"> <input type="radio" name="radio6" value="Active"> <font color="green">Active</font> <span>&nbsp</span> </label> <label class="kt-radio"> <input type="radio" name="radio6" value="Non-Active" checked> <font color="orange">Non-Active</font> <span>&nbsp</span> </label> <label class="kt-radio"> <input type="radio" name="radio6" value="Disabled"> Disabled <span>&nbsp</span> </label> </div> </div> </div> </div> <div class="row"> <div class="col-md-4"> <div class="form-group form-md-line-input has-danger" style=" margin-bottom: 15px"> <div class="input-icon"> <input type="text" class="form-control" name="lname" required style="padding-left: 10px"> <label for="form_control_1" style="font-weight: bolder; font-size: 11px">LAST NAME</label> <span class="help-block" style="font-size: 10px">Insert Last Name...</span> </div> </div> </div> <div class="col-md-4"> <div class="form-group form-md-line-input has-danger" style=" margin-bottom: 15px"> <div class="input-icon"> <input type="text" class="form-control" name="firname" required style="padding-left: 5px"> <label for="form_control_1" style="font-weight: bolder; font-size: 11px">FIRST NAME</label> <span class="help-block" style="font-size: 10px">Insert First Name...</span> </div> </div> </div> <div class="col-md-3"> <div class="form-group form-md-line-input has-danger" style=" margin-bottom: 15px"> <div class="input-icon"> <input type="text" class="form-control" name="mname" style="padding-left: 5px; "> <label for="form_control_1" style="font-weight: bolder; font-size: 11px">INITIAL</label> <span class="help-block" style="font-size: 10px">Insert Initial...</span> </div> </div> </div> </div> <div class="row"> <div class="col-md-4"> <div class="form-group form-md-line-input has-danger"> <div class="input-icon"> <input type="text" class="form-control" name="department" required style="padding-left: 10px"> <label style="font-weight: bolder; font-size: 11px; font-family: Arial Narrow ">DEPARTMENT</label> <span class="help-block" style="font-size: 10px">Insert Department Name...</span> </div> </div> </div> <div class="col-md-8"> <div class="form-group form-md-line-input has-danger"> <div class="input-icon"> <input type="text" class="form-control" name="Jo_title" required style="padding-left: 5px"> <label style="font-weight: bolder; font-size: 11px; font-family: Arial Narrow ">JOB TITLE</label> <span class="help-block" style="font-size: 10px">Insert your job title...</span> </div> </div> </div> </div> <div class="note note-danger"> <h5 style="font-family: Century Gothic"> More Information Here: <div class="form-group form-md-line-input has-danger"> <div class="input-icon"> <input type="text" class="form-control" name="Mor_Desc" style="padding-left: 5px"> <label style="font-weight: bolder; font-size: 11px; font-family: Arial Narrow ">ADDITIONAL INFORMATION ABOUT THE EMPLOYEE</label> <span class="help-block" style="font-size: 10px">Insert Short Description...</span> <!--<i class="fa fa-info"></i>--> </div> </div> </h5> </div> <?php }else{ ?> <?php } ?> </div>可能是最快的,不需要临时文件,因此python包装器将是:

sed