日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/7/5 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python删除指定行_关于csv:删除python中的特定行和对应文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我想刪除90%的"轉向"值等于0的行。這三個圖像都有一個對應的圖像文件,中間,左邊和右邊。我也要刪除它們。csv文件如下:

我編寫了以下代碼,以至少獲取轉向值為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個文件中的每一個。

總結

以上是生活随笔為你收集整理的python删除指定行_关于csv:删除python中的特定行和对应文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。