Python:根据现有列将列添加到CSV文件 [英] Python: add column to CSV file based on existing column

查看:284
本文介绍了Python:根据现有列将列添加到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了我需要识别和解析我正在寻找的值,我需要帮助写一个列到csv文件(或一个新的csv文件)与解析的值。这里是一些伪代码/有点现实的Python代码为我的努力:

I already have written what I need for identifying and parsing the value I am seeking, I need help writing a column to the csv file (or a new csv file) with the parsed value. Here's some pseudocode / somewhat realistic Python code for what I am trying to do:

# Given a CSV file, this function creates a new CSV file with all values parsed
def handleCSVfile(csvfile):
  with open(csvfile, 'rb') as file:
    reader = csv.reader(file, delimiter=',', lineterminator='\n')
    for row in reader:
        for field in row:
          if isWhatIWant(field):
            parsedValue = parse(field)
            # write new column to row containing parsed value

我已经写了 isWhatIWant parse 函数。如果我需要写一个全新的csv文件,那么我不知道如何同时打开和读写一个到另一个。

I've already written the isWhatIWant and parse functions. If I need to write a completely new csv file, then I am not sure how to have both open simultaneously and read and write from one into the other.

推荐答案

我会这样做。我猜想 isWhatIWant()是应该在现场替换字段的东西。

I'd do it like this. I'm guessing that isWhatIWant() is something that is supposed to replace a field in-place.

import csv

def handleCSVfile(infilename, outfilename):
    with open(infilename, 'rb') as infile:
        with open(outfilename, 'wb') as outfile:
            reader = csv.reader(infile, lineterminator='\n')
            writer = csv.writer(outfile, lineterminator='\n')

            for row in reader:
                for field_index, field in enumerate(row):
                    if isWhatIWant(field):
                        row[field_index] = parse(field)
                writer.writerow(row)

这种模式出现很多, 。有时可能有助于将打开和文件的逻辑打开成不同的函数,例如:

This sort of pattern occurs a lot and results in really long lines. It can sometimes be helpful to break out the logic from opening and files into a different function, like this:

import csv

def load_save_csvfile(infilename, outfilename):
    with open(infilename, 'rb') as infile:
        with open(outfilename, 'wb') as outfile:
            reader = csv.reader(infile, lineterminator='\n')
            writer = csv.writer(outfile, lineterminator='\n')

            read_write_csvfile(reader, writer)

def read_write_csvfile(reader, writer)
    for row in reader:
        for field_index, field in enumerate(row):
            if isWhatIWant(field):
                row[field_index] = parse(field)
        writer.writerow(row)

这个模块化了代码,使你更容易从相互独立的逻辑中改变处理文件和格式的方式。

This modularizes the code, making it easier for you to change the way the files and formats are handled from the logic independently from each other.

其他提示:


  • 不要命名变量 file ,因为这是一个内置函数。

  • delimiter =','是默认值,因此您不需要以明确指定。

  • Don't name variables file as that is a built-in function. Shadowing those names will bite you when you least expect it.
  • delimiter=',' is the default so you don't need to specify it explicitly.

这篇关于Python:根据现有列将列添加到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆