删除已使用的行

2024-09-28 03:24:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个50万行的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()

Tags: 文件csvcsvfilepath代码textimportos
2条回答

达特霍普托,如果你想遍历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()

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

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]

相关问题 更多 >

    热门问题