简体   繁体   English

删除 csv 文件中的行或单元格

[英]Delete row or cell in a csv file

I want to delete rows from a csv file as they are processed.我想在处理 csv 文件时删除行。 My file:我的文件:

 Sr,Name1,Name2,Name3
 1,Zname1,Zname2,Zname3
 2,Yname1,Yname2,Yname3
 3,Xname1,Xname2,Xname3

I want to read row by row and delete the row which has been processed.我想逐行读取并删除已处理的行。 So the file will be now:所以文件现在是:

2,Yname1,Yname2,Yname3
3,Xname1,Xname2,Xname3

The solutions which are provided on other questions are:针对其他问题提供的解决方案是:

  1. read the file读取文件
  2. use next() or any other way to skip the row and write the remaining rows in an updated file使用next()或任何其他方式跳过该行并将剩余的行写入更新的文件中

I want to delete the row from the original file which was entered in .reader() method我想从.reader()方法中输入的原始文件中删除该行

My code:我的代码:

 with open("file.txt", "r") as file
 reader = csv.reader(file)
 for row in reader:
     #process the row
     #delete the row

I have not been able to figure out how to delete/remove the row.我无法弄清楚如何删除/删除该行。

I want the change to be in the original file.txt because I will be running the program many times and so each time it runs, file.txt will already be present and the program will start from where it ended the last time.我希望更改位于原始file.txt中,因为我将多次运行该程序,因此每次运行时, file.txt已经存在,并且程序将从上次结束的位置开始。

Just read the csv file in memory as a list, then edit that list, and then write it back to the csv file.只需读取 memory 中的 csv 文件作为列表,然后编辑该列表,然后将其写回 csv 文件。

lines = list()

members= input("Please enter a member's name to be deleted.")

with open('mycsv.csv', 'r') as readFile:

    reader = csv.reader(readFile)

    for row in reader:

        lines.append(row)

        for field in row:

            if field == members:

                lines.remove(row)

with open('mycsv.csv', 'w') as writeFile:

    writer = csv.writer(writeFile)

    writer.writerows(lines)

You can delete column like this:您可以像这样删除列:

We can use the panda pop () method to remove columns from CSV by naming the column as an argument.我们可以使用 panda pop() 方法通过将列命名为参数来从 CSV 中删除列。 Import Pandas.进口Pandas。 Read CSV File.读取 CSV 文件。 Use pop() function for removing or deleting rows or columns from the CSV files.使用 pop() function 从 CSV 文件中删除或删除行或列。 Print Data.打印数据。

You probably can find inspiration here: How to delete a specific line in a file?您可能可以在这里找到灵感: 如何删除文件中的特定行? . .

And don't forget to grant write permission when opening the file.并且不要忘记在打开文件时授予写权限。

since pandas package deals in big data, there is no solution in basic python.由于 pandas package 处理大数据,因此基本的 python 没有解决方案。 You will have to import pandas.您必须导入 pandas。

import python
df=pandas.read_csv("file_name.txt")
df.set_value(0,"Name3",new_value)
df.to_csv("file_name.txt", index=False) 

this code edits the cell in 0th row and Name3 column.此代码编辑第 0 行和 Name3 列中的单元格。 0th row is the first row below the header.第 0 行是 header 下方的第一行。 thus, Zname3 will be changed to something else.因此, Zname3 将更改为其他内容。 You can similarily delete a row or a cell.您可以类似地删除一行或一个单元格。

I have not tried this code but it is supposed to work in the required manner.我没有尝试过这段代码,但它应该以所需的方式工作。

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

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