python删除指定行_关于csv:删除python中的特定行和对应文件

我想删除90%的"转向"值等于0的行。这三个图像都有一个对应的图像文件,中间,左边和右边。我也要删除它们。csv文件如下:

abmS9.png

我编写了以下代码,以至少获取转向值为0的文件。我所需要的就是随机获取90%的文件并删除它们的代码。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18with 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

您是否搜索过如何在python中生成随机数和删除文件?生成随机数。删除文件。

是的,我需要随机导入并使用os.remove()删除文件。但是,我在两个地方感到困惑,从csv文件中删除一行,随机删除90%的转向值等于0的文件。

你能把你的csv文件的一部分以文本格式发布吗?最好用逗号作为分隔符。

而且,您的代码是不完整的。很多未定义的变量。

img/中_2016_12_01_13_30_48_287.jpg,img/左_2016_12_01_13_48_287.jpg,img/右_2016_12_01_13_48_287.jpg,0,0,0,22.14829。

我添加了代码开始工作的部分。在此之前,它只是导入包并初始化变量。

我认为这会满足你的需求:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39import 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的条件。另外,我还想删除相应的文件。就像我想删除img文件夹中的center_2016_12_01_13_30_48_287.jpg。你能帮我做这两件事吗?

这行if lines[index] not in toKeep and lines[index][3] == '0':指出,如果我们还没有将该行添加到要保留的行列表中,并且如果转向柱等于0,那么……

这些行:for i in range(3):和remove(line[i])将删除该行的3个文件中的每一个。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要删除csv文件特定,您可以使用Pythoncsv模块来读取和写入csv文件。以下是一些示例代码,可以帮助您删除csv文件特定: ```python import csv # 读取csv文件 with open('example.csv', 'r') as csvfile: reader = csv.reader(csvfile) # 创建一个新的csv文件来保存修改后的数据 with open('new_example.csv', 'w', newline='') as new_csvfile: writer = csv.writer(new_csvfile) # 遍历每一数据并删除指定 for row in reader: # 假设要删除第三数据 if reader.line_num == 3: continue # 跳过第三数据 else: writer.writerow(row) ``` 在上面的示例代码,我们使用了两个csv文件:一个用于读取原始数据,另一个用于保存修改后的数据。我们遍历每一数据并检查是否为要删除。如果是,则跳过该数据。否则,将该数据写入新的csv文件。最终,您将获得一个不包含特定csv文件。 ### 回答2: 在Python,要删除CSV文件特定,可以使用`csv`模块进操作。 首先,你需要导入`csv`模块,并打开原始CSV文件和一个新的CSV文件,用于存储删除特定后的数据。 ```python import csv # 打开原始CSV文件和新的CSV文件 with open('原始文件.csv', 'r') as file, open('新文件.csv', 'w', newline='') as new_file: reader = csv.reader(file) writer = csv.writer(new_file) # 遍历原始文件的每一 for row in reader: # 根据特定的条件判断是否需要删除 if 条件成立: continue # 如果条件成立,则跳过该 # 将不需要删除写入新文件 writer.writerow(row) ``` 在上述代码,你需要修改 '原始文件.csv' 和 '新文件.csv' 为实际文件名。然后,你需要根据特定的条件来判断是否需要删除。如果条件成立,则使用`continue`语句跳过该;否则,将写入新文件。 请根据你的具体需求修改上述代码,并使用合适的条件来删除特定。 ### 回答3: 要删除CSV文件特定,可以使用Pythoncsv模块来读取和写入CSV文件。下面是一个示例代码来实现这个功能: ```python import csv def delete_csv_row(filename, row_nums): # 读取CSV文件内容 with open(filename, 'r', newline='') as csvfile: reader = csv.reader(csvfile) rows = list(reader) # 删除特定 for row_num in sorted(row_nums, reverse=True): del rows[row_num] # 保存修改后的CSV文件 with open(filename, 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerows(rows) # 删除CSV文件的第2和第4 delete_csv_row('data.csv', [1, 3]) ``` 以上代码首先使用csv.reader()函数读取CSV文件的内容,并将其保存到一个二维列表rows。然后,根据指定号(row_nums),使用del语句删除这些。最后,使用csv.writer()函数将修改后的内容写回到CSV文件。 需要注意的是,号是从0开始计数的。在上述示例删除的是第2和第4对应号是1和3。根据实际情况,可以修改row_nums来删除特定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值