如何命令 CSV 从该行开始删除该行和该行的行



我在实现函数时遇到问题。

首先是片段:

# LOAD ROWS
for row in eans:
gtin_row = row[0]
if(gtin_row[0].isdigit()):
print('True')
gtin = row[0].split(";")
gtin = gtin[0]
# DIGIT IS FOUND IN row[0] so START at the next row
next(eans,None)
# IF ITEM IS FOUND on Found_list
if gtin in found_list:
# STARTING FROM row[0] I mentioned earlier until it finds another row that starts with a digit, delete every row
if(row[0].isdigit):
break
else:
print('Non Digit = Iterate')
output = open(csv_file, 'wb')
writer = csv.writer(output)
writer.writerow(row)                    
output.close()
next(eans,None)
continue

我正在加载一个列表,从一个我评论代码应该如何工作的csv,不知何故next()不起作用。我在开始next(eans,None)时遇到问题,因为它没有将rowean 指向下一行。

假设我有这样的行:

3423423

Jalsjfslv

FBDBDF

BDFNDF

32532532

我打算从jalsjfslv删除它及其后续行开始,直到它指向if(row[0].isdigit):应该工作32532532并停止循环,以便它可以继续下一个gtin

我的代码不适用于.nextnext(list, None)知道如何解决这个问题的任何想法?真的卡住了,我想我错过了一些东西。

谢谢!

最好的办法是完全避免使用下一条语句。以下代码使用更简单的概念完成任务。

# LOAD ROWS
import csv
csv_file = "aFile.csv"
# The above file is blank. 
found_list = ["3423423"]
eans = ["3423423", "jalsjfslv", "fbdbdf", "bdfndf", "32532532"]
clear = False
output = open(csv_file, 'w', newline='')
output.close()
for row in eans:
if(row.isdigit()):
clear = False
output = open(csv_file, 'a', newline='')
writer = csv.writer(output)
writer.writerow([row])   
output.close()
print('True')
gtin = row.split(";")
gtin = gtin[0]
if gtin in found_list:
clear = True
elif(clear == True):
print('Non Digit = Iterate')

请注意,发布的相当多的代码似乎脱离了上下文。例如,对前一个值的不断索引似乎阻碍了任务的完成。 使用此概念来适应您认为合适的代码。

最新更新