2017-04-24 78 views
0

我想删除90%,其中有“转向”价值QUAL为0并且对于所有三架F其中,中心相应的图像文件行,左,右的。我也想删除它们。 csv文件是这样的:enter image description here删除特定行和相应的文件在python

我写了下面的代码,至少得到的文件有转向价值0.我所需要的是随机获得这些文件的90%,并删除它们的代码。

with open('data/driving_log.csv') as csvfile: 
    reader = csv.reader(csvfile) 
    for i,line in enumerate(reader): 
     lines.append(line) 
     index.append(i) 

lines = np.delete(lines,(0), axis = 0) 
for i, line in enumerate(lines): 
    #print(type(line[3].astype(np.float))) 
    line_no.append(line[3].astype(np.float32)) 
    #print(line_no[i]) 
    if line_no[i]==0.0: 
      # this gets the first column of the row. 
     for j in range(3): 
      source_path = line[j] 
      filename = source_path.split('/')[-1] 
      print(filename) 
     count += 1 
+0

你有搜索如何生成随机数和在Python删除文件? [产生随机数(http://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9)。 [删除文件(http://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder)。 –

+0

是的,我需要导入随机并使用os.remove()删除文件。但是,我越来越感到困惑在两个地方,移除一行形成csv文件,并随机删除具有转向值等于0 –

+0

您可以发布文本格式的CSV文件的部分文件的90%?最好用逗号作为分隔符。 –

回答

0

我认为这会做你想要什么:

import csv 
from random import randint 
from os import remove 

# Create a 2D list from which we can work with 
lines = [] 
with open('data/driving_log.csv', newline='') as csvfile: 
    reader = csv.reader(csvfile) 
    for line in reader: 
     lines.append(line) 

# Find 10% of total lines (to keep), not including header row 
numToKeep = round(sum(1 for i in lines if i[3] == '0') * 0.1) 

# Save 10% of lines to a new 2D list 
toKeep = [] 
for i in range(numToKeep): 
    while True: 
     index = randint(1, len(lines)-1) 
     # Make sure we haven't already selected the same line 
     if lines[index] not in toKeep and lines[index][3] == '0': 
      toKeep.append(lines[index]) 
      break 

# Deleting all files of the selected 90% of rows 
for i, line in enumerate(lines): 
    if i == 0: # Omit the header row 
     continue 
    if lines[i][3] != '0': # Keep rows that don't have a steering value of 0 
     toKeep.append(lines[i]) 
    if line not in toKeep: 
     print("Deleting: {}".format(line)) 
     for i in range(3): 
      remove(line[i]) 

with open('data/driving_log.csv', 'w', newline='') as csvfile: 
    writer = csv.writer(csvfile) 
    writer.writerows([lines[0]]) # Put the header back in 
    writer.writerows(toKeep) 

我知道这是不是最完美的解决方案。我对numpy不熟悉,现在没有时间去学习,但这应该起作用。

+0

嘿,谢谢你。但是我没有看到转向角度需要为0的情况。另外,我还想删除相应的文件。就像我想删除IMG文件夹中的'center_2016_12_01_13_30_48_287.jpg'。你能帮我解决这两件事吗? –

+0

如果lines [index]不在toKeep和lines [index] [3] =='0'中,该行'如果我们还没有将该行添加到我们的行列表以保持AND,并且转向列等于0,那么... –

+0

这些行:'为我在范围(3):'AND'删除(行[i])'将删除该特定行的3个文件中的每一个。 –