重新格式化数据并写入新的

重新格式化数据并写入新的

本文介绍了在 python 2.7 中 - 如何从 csv 读取数据、重新格式化数据并写入新的 csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个编程新手,我被卡住了...我的目标是打开一个 csv 文件(从程序 A 导出),重新格式化数据,并将其写入一个新的 csv 文件(对于程序 B)导入).我知道我的代码并不漂亮,尽管它在写入新的 csv 文件之前一直有效.它只写入旧 csv 中的最后一行数据.

I'm a programing noob, and I'm stuck... My goal is to open a csv file (an export from program A), reformat the data, and write it to a new csv file (for program B to import). I know my code isn't pretty, though it works up until it writes to the new csv file. It only writes the last row of data from the old csv.

import csv

print 'Enter file location or press enter for default location.'
aPath = raw_input('> ')  # user path to file
if aPath == '':
    aPath = '/default/path/to/file'  # default path
aFile = raw_input('Enter file name: ')  # user file name
if aFile == '':
    aFile = 'orginal.csv'  # default file name
myCSV = csv.DictReader(open(aPath + aFile, 'r'), delimiter=',')
print myCSV.fieldnames # verify fieldnames

for p in myCSV:
    w = csv.writer(open("output.csv", "w"))
    try:
        p = dict((k, v) for k, v in p.iteritems()
            if v.lower() != 'null')
    except AttributeError, e:
        print e
        print p
        raise Exception()
# reformats the columns of data for the output.csv
    pID = p.get('Last Name')[-4:]
    pName = p.get('Last Name')[:-4].strip() + ', ' + p.get('First Name')
    pDate = p.get('Start Time')[:-5]
    pBlank = p.get('')
    pCourse = p.get('Assigned Through')
    pScore = p.get('Score')[:-1]

# verifies the new columns
    print pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore

# NOT working...Only writes the last row of the orginal.csv to output.csv
    w.writerow([pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore])

推荐答案

以下修改后的代码应该可以工作:

The following modified code should work:

import csv

print 'Enter file location or press enter for default location.'
aPath = raw_input('> ')  # user path to file
if aPath == '':
    aPath = '/default/path/to/file'  # default path
aFile = raw_input('Enter file name: ')  # user file name
if aFile == '':
    aFile = 'orginal.csv'  # default file name
myCSV = csv.DictReader(open(aPath + aFile, 'r'), delimiter=',')
print myCSV.fieldnames # verify fieldnames

with open("output.csv", "wb") as ofile:
    w = csv.writer(ofile)
    for p in myCSV:
        try:
            p = dict((k, v) for k, v in p.iteritems()
                if v.lower() != 'null')
        except AttributeError, e:
            print e
            print p
            raise Exception()
    # reformats the columns of data for the output.csv
        pID = p.get('Last Name')[-4:]
        pName = p.get('Last Name')[:-4].strip() + ', ' + p.get('First Name')
        pDate = p.get('Start Time')[:-5]
        pBlank = p.get('')
        pCourse = p.get('Assigned Through')
        pScore = p.get('Score')[:-1]

    # verifies the new columns
        print pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore

    # NOT working...Only writes the last row of the orginal.csv to output.csv
        w.writerow([pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore])

我们改变的东西:

  1. 我们使用了 with 运算符.在处理打开和关闭文件时,使用 with 是普遍接受的协议,除非另外不适用,因为它可以正确处理文件关闭等.
  2. 使用 wb 而不是 w 作为 open 的第二个参数.这将允许您以写入二进制模式打开文件.请参阅以供参考.
  1. We used the with operator. When working with opening and closing files, using with is a generally accepted protocol to follow unless otherwise inapplicable since it handles the closing of files properly, among other things.
  2. Use wb instead of just w as your second argument to open. This will allow you to open the file in write-binary mode. See this for reference.

希望这会有所帮助.

这篇关于在 python 2.7 中 - 如何从 csv 读取数据、重新格式化数据并写入新的 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:49