[英]In python 2.7 - How to read data from csv, reformat data, and write to new csv
我是一個正在編程的菜鳥,我被困住了...我的目標是打開一個csv文件(從程序A導出),重新格式化數據,並將其寫入新的csv文件中(以供程序B導入) 。 我知道我的代碼不是很漂亮,盡管它可以工作到寫入新的csv文件。 它僅寫入舊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])
您將重新打開文件,並for p in myCSV
循環中的for p in myCSV
每次傳遞時將其覆蓋。 進入for循環之前,您需要創建一次w
。
以下修改后的代碼應該可以工作:
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])
我們改變的事情:
with
運算符。 當使用打開和關閉文件進行操作時,遵循with
是一種普遍接受的協議,除非另外不適用,因為它可以正確處理文件的關閉等操作。 wb
而不是僅將w
作為open
的第二個參數。 這將允許您以寫二進制模式打開文件。 請參閱此作為參考。 希望這可以幫助。
恐怕您會在每次迭代時重新編寫輸出文件...您應該簡單地選擇這一行
w = csv.writer(open("output.csv", "w"))
跳出循環為:
w = csv.writer(open("output.csv", "w"))
try:
for p in myCSV:
try:
...
w.writerow([pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore])
finally:
w.close()
您還可以查看“帶有”結構,該結構可自動處理潛在的io錯誤,並在塊末尾關閉文件,而無需進行其他嘗試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.