使用后删除一行

时间:2014-02-04 20:46:21

标签: python

我有一个500k行的CSV。目前我只需要使用文件中的一个值,我就可以得到它。我想要做的是,一旦使用该值删除行,这可能吗?

这是我目前的代码:

import csv
import os

def getvin():
    linenumber = 1
    with open(os.path.join(os.path.dirname(__file__), 'ITRACK6VIN.csv'), 'rb') as csvfile:
        vinreader = csv.reader(csvfile)
        myvins = list(vinreader)
        text = myvins[linenumber][0]
        return text

print getvin()

2 个答案:

答案 0 :(得分:2)

DarthOpto,如果你想迭代vin数字,那么你可以做类似的事情

import csv
import os

def process_vins():
    vin_numbers = []
    with open(os.path.join(os.path.dirname(__file__), 'ITRACK6VIN.csv'), 'r') as csvfile:
        vinreader = csv.reader(csvfile)
        vinreader.next() # skip the first row, presumably a header
        for row in vinreader: # iterate through each row in the file
            current_vin = row[0] # access the first element of the row
            current_vin = modify_vin(current_vin) # optional, do something with vin
            vin_numbers.append(current_vin) # store vins in a list

    return vin_numbers # return that list

process_vins()

答案 1 :(得分:0)

听起来你想要这样的东西:

import csv
import os

with open(os.path.join(os.path.dirname(__file__), 'ITRACK6VIN.csv'), 'rb') as csvfile:
    vinreader = csv.reader(csvfile)
    for line in vinreader:
        print line[0]