python - 删除 CSV 中某些列中的字段

标签 python csv

我需要在第 8 列之后的每条记录中查找重复值,如果有,请将其删除。每条记录的前两个值可以相同,不能删除。

with open("User.csv") as file:
    reader = csv.reader(file)
    for row in reader:
        for column in reader:
            print(column)

上面的代码读取控制台中的文件:

['257488', '257488', '3', '1234', '', '1', '1', '', '160000', '', '0', '', '']
['257488', '257488', '3', '1234', '', '1', '1', '', '160000', '', '0', '', '']
['270076', '270076', '2', '1234', '', '1', '1', '', '40000', '270076CASH', '270076CASH', '', '']
['270076', '270076', '2', '1234', '', '1', '1', '', '40000', '270076CASH', '0', '', '']

注意:上面第 3 行中的第二个值“270076CASH”要删除。它应该检查并删除其余行。

最佳答案

解决此问题的一个解决方案是获取第 8 列之后的所有项目,并仅存储唯一的项目(通过标准 for 循环和 if 语句进行检查)。然后,取出第 8 列后的这些唯一项目并将它们插入回记录中。下面是一个相当多评论的实现:

with open( 'User.csv' ) as file:
    reader = csv.reader( file )
    for record in reader:

        # store all items after column 8 into a temporary list
        subset_list = record[9:]

        # keep track of unique (non-duplicate) items in the subset
        unique_subset = []

        # loop through every item of the subset
        for item in subset_list:

            # check if the current item already exists, then append
            if not item in unique_subset or item == '':
                unique_subset.append( item )

        # after looping through subset, reinsert the unique entries into column
        new_record = record[:9] + unique_subset[:]

        # verify operations
        print( new_record )

关于python - 删除 CSV 中某些列中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172943/

相关文章:

python - UDP Socket 没有收到任何带有 python 的消息

excel - 如何在不自动连接的情况下将数据导入Excel?

Python:使用 pandas 导入 csv。尝试绘制一列,但出现错误,提示 "no numerical data to plot"

ruby-on-rails - 在 Ruby 中,如何从 CSV 文件中按列读取数据?

c# - CsvHelper 解析 csv 并将字符串转换为 DateTime

python - 读取 csv 时 csv.DictReader 使用哪种编码?

Python:如何处理?

Python:内联 if 语句 else 什么都不做

python - 如何在 Python/Django 中将 MP4 转换为具有多种音频和多种视频质量的 HLS?

Python 最小覆盖