简体   繁体   English

如何在循环遍历每一行以运行 function 时删除 CSV 文件中的一行?

[英]How to remove a row in a CSV file while looping through each row to run a function?

I have a program that will loop through a CSV file and execute a function on each line in the CSV to perform a task.我有一个程序将遍历 CSV 文件并在 CSV 的每一行上执行 function 以执行任务。 Once that task his performed, I want to be able to remove that line in the CSV to be able to keep track of what was changed while the script is running.一旦他执行了该任务,我希望能够删除 CSV 中的该行,以便能够跟踪脚本运行时发生的更改。 Below is the part of the code that loops through the CSV file下面是循环通过 CSV 文件的部分代码

def migrate_repo(team_name, gh_token):
    with open('Repositories.csv', 'r') as csv_file:
        reader = csv.reader(csv_file)
        for x in reader:
            print("repository: " + str(x))
            print("1) Migrate repo")
            print("2) Skip repo")
            print("3) Exit")
            a = input("Please choose an option above: ")

The CSV looks like this but each URL is different. CSV 看起来像这样,但每个 URL 是不同的。 There are also no headers and its only in one column:也没有标题,只有一列:

http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
http://ajdhfajdhfasdhflkjashdflkjahsdfjasdl
etc.

I want to be able to remove the row after the while loop finishes its task on that specific row and moves onto the next row.我希望能够在 while 循环在该特定行上完成其任务并移动到下一行之后删除该行。

As @OneCriketeer pointed out, the only way to "modify" a file is to completely overwrite it with the modified data.正如@OneCriketeer 指出的那样,“修改”文件的唯一方法是用修改后的数据完全覆盖它。 To that end, I propose:为此,我提议:

  1. Reading all URLs from your repository CSV into a list将存储库 CSV 中的所有 URL 读取到列表中
  2. Process the list, keeping track of successful executions of your function处理列表,跟踪 function 的成功执行
  3. Subtract your processed URLs from the original list, leaving you with unprocessed从原始列表中减去已处理的 URL,留下未处理的 URL
  4. Write over you repository with the list of just unprocessed用刚刚未处理的列表覆盖您的存储库

All that effectively deletes processed lines from the original.所有这些都有效地从原始文件中删除了已处理的行。

I cannot run this, so there may be a few typos in it, but here's the general idea:我无法运行此程序,因此其中可能存在一些拼写错误,但总体思路如下:

repositories = []
with open('Repositories.csv', newline='') as csv_file:
    reader = csv.reader(csv_file)
    repositories = list(reader)

processed = []
for repo in repositories:
    print("repository: " + repo)   # coming straight out of a CSV, values are always strings, no str() conversion required
    print("1) Migrate repo")
    print("2) Skip repo")
    print("3) Exit")
    a = input("Please choose an option above: ")
    # ... do stuff
    # ... finally:
    processed.append(repo)

# Use set() to "subtract" one list from another
unprocessed = set(repositories) - set(processed)

# unprocessed is now a set, still iterable, but convert back to a list if you like
# unprocessed = list(unprocessed)

with open('Repositories.csv', 'w', newline='') as csv_file:
    reader = csv.writer(csv_file)
    writer.writerows(unprocessed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM