初学者从CSV中删除列(无Pandas)

2024-10-03 21:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚开始编写代码,我正在尝试从项目的CSV中删除某些列,我们不应该使用pandas。例如,我必须删除的一个字段名为DwTm,但是我必须删除大约15列;我只需要前几个,下面是我得到的:

import csv
FTemp = "D:/tempfile.csv"
FOut = "D:/NewFile.csv"


with open(FTemp, 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    with open(FOut, 'w') as new_file:
        fieldnames = ['Stn_Name', 'Lat', 'Long', 'Prov', 'Tm']
        csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)

        for line in csv_reader:
            del line['DwTm']
            csv_writer.writerow(line)

当我运行这个,我得到的错误

del line['DwTm']
TypeError: list indices must be integers or slices, not str

这是我发现的唯一一种不用熊猫的方法。有什么想法吗?你知道吗


Tags: csv代码newaswithlineopenreader
3条回答

最简单的方法是使用DictReader读取文件。与用于编写文件的DictWriter一样,DictReader对行使用字典,因此从旧行中删除键然后写入新文件的方法将按预期工作。你知道吗

import csv
FTemp = "D:/tempfile.csv"
FOut = "D:/NewFile.csv"


with open(FTemp, 'r') as csv_file:

    # Adjust the list to be have the correct order
    old_fieldnames = ['Stn_Name', 'Lat', 'Long', 'Prov', 'Tm', 'DwTm']
    csv_reader = csv.DictReader(csv_file, fieldnames=old_fieldnames)

    with open(FOut, 'w') as new_file:
        fieldnames = ['Stn_Name', 'Lat', 'Long', 'Prov', 'Tm']
        csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)

        for line in csv_reader:
            del line['DwTm']
            csv_writer.writerow(line)

可以同时打开要读取的文件和要写入的文件。假设您知道要保留的列的索引,例如0、2和4:

good_cols = (0,2,4)
with open(Ftemp, 'r') as fin, open(Fout, 'w') as fout:
    for line in fin:
        line = line.rstrip()        #clean up newlines
        temp = line.split(',')      #make a list from the line
        data = [temp[x] for x in range(len(temp)) if x in good_cols]
        fout.write(','.join(data) + '\n')

列表理解(数据)只从每一行中提取要保留的列,并使用join方法(加上为每一新行添加一个结束行)立即逐行写入新文件。你知道吗

如果您只知道要保留/删除的字段的名称,则需要从csv文件的第一行提取索引,但这并不困难。你知道吗

下面

import csv

# We only want to read the 'department' field 
# We are not interested in 'name' and 'birthday month'

# Make sure the list items are in ascending order
NON_INTERESTING_FIELDS_IDX = [2,0]
rows = []
with open('example.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        for idx in NON_INTERESTING_FIELDS_IDX:
          del row[idx]
        rows.append(','.join(row))
with open('example_out.csv','w') as out:
  for row in rows:
    out.write(row + '\n')

你知道吗示例.csv你知道吗

name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March

示例_输出.csv你知道吗

department
Accounting
IT

相关问题 更多 >