[英]Overwrite a specific column in a csv file using Python csv module
I am using Python csv module to read a csv file with every line being like: 我使用Python csv模块读取csv文件,每行都是这样的:
2013-04-16 7:11:01,186744,3,2,2,1.89E-03
I then convert row[0] to unix time but then I want to replace the datetime with the unix time I just found for every row of my csv file 然后我将row [0]转换为unix时间,但后来我想用我刚刚为csv文件的每一行找到的unix时间替换datetime
import pymongo
import datetime
import re
import csv
import calendar
X = []
OBD = []
Y = []
csv_in = open('FakeAPData.csv', 'rb')
for row in reader:
date = datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
datet = unicode(datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S'))
datett = tuple(int(v) for v in re.findall("[0-9]+", datet))
y = calendar.timegm(datett)
Y.append(y)
So I create the list Y with the unixtime values but then how do I do the replacement so as to have an output like that: 所以我使用unixtime值创建列表Y但是我如何进行替换以获得类似的输出:
1366097085,186744,3,2,2,1.89E-03
Each row
is just a list
. 每row
只是一个list
。 You can modify it in-place, or create a new list with the value you want substituted out: 您可以就地修改它,或者创建一个包含要替换的值的新列表:
row[0] = y # or row = [y] + row[1:], or ...
If you want to write it back to a file, you need to use a csv.writer
for that. 如果要将其写回文件,则需要使用csv.writer
。 For example: 例如:
os.rename('FakeAPData.csv', 'FakeAPData.csv.bak')
csv_in = open('FakeAPData.csv.bak', 'rb')
csv_out = open('FakeAPData.csv', 'wb')
writer = csv.writer(csv_out)
for row in csv.reader(csv_in):
date = datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
datet = unicode(datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S'))
datett = tuple(int(v) for v in re.findall("[0-9]+", datet))
y = calendar.timegm(datett)
row[0] = y
writer.writerow(row)
Of course you'll also want to close
your files, and clean up all the repeated and unused code. 当然,您还需要close
文件,并清理所有重复和未使用的代码。 While we're at it, I'd factor out the date-transforming code into a function. 在我们处理它时,我会将日期转换代码分解为函数。 And use functions that make it easy, instead of ones that make it difficult and fragile. 并使用易于使用的功能,而不是使其变得困难和脆弱的功能。
So: 所以:
def transform_date(date):
return calendar.gmtime(datetime.strptime(date, '%Y-%m-%d %H:%M:%S').timetuple())
def transform_row(row):
return [transform_date(row[0])] + row[1:]
name = 'FakeAPData.csv'
bakname = name + '.bak'
os.rename(name, bakname)
with open(bakname, 'rb') as in csv_in, open(name, 'wb') as csv_out:
writer = csv.writer(csv_out)
writer.writerows(transform_row(row) for row in csv.reader(csv_in))
First of all, there are better ways to convert a textual date-time format into a UNIX timestamp. 首先,有更好的方法将文本日期时间格式转换为UNIX时间戳。 Direct use of the time
module simplifies your code to: 直接使用time
模块可将您的代码简化为:
import time
import calendar
timestamp = calendar.gmtime(time.strptime(row[0], '%Y-%m-%d %H:%M:%S'))
but even the datetime
object you created has .timetuple()
and .utctimetuple()
methods that would be miles more reliable at producing a time_struct
tuple than parsing the string format of the datetime
object back to a tuple of integers. 但即使是你创建的datetime
对象也有.timetuple()
和.utctimetuple()
方法,这些方法在生成time_struct
元组时比将datetime
对象的字符串格式解析回整数元组更加可靠。 You may as well do that directly on row[0]
as the output of str(datetime.now())
is the same format as what you started with. 您不妨这样做直接 row[0]
作为输出str(datetime.now())
是相同的格式,你开始用什么。
Next, write out a new file and replace the old one with it once done: 接下来,写完一个新文件,并在完成后用它替换旧文件:
import csv
import time
import calendar
import os
with open('FakeAPData.csv', 'rb') as infile, open('FakeAPData.csv.new', 'wb') as outfile:
writer = csv.writer(outfile)
for row in csv.reader(infile):
timestamp = calendar.gmtime(time.strptime(row[0], '%Y-%m-%d %H:%M:%S'))
writer.writerow([timestamp] + row[1:])
os.rename('FakeAPData.csv.new', 'FakeAPData.csv')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.