[英]Iterate each integer in row in CSV column Python
The csv file has each row in the following format: csv文件的每一行都采用以下格式:
527131607.9 Google Maps
Where there is a total of 2 columns. 总共有2列。 For this, we are only interested in the first column.
为此,我们只对第一列感兴趣。 I have been using the code:
我一直在使用代码:
import datetime
with open("user1_nsdate.csv",'r') as f:
for row in f:
for t, val in enumerate(row):
time = datetime.datetime.fromtimestamp(t+978307200).strftime('%Y-%m-%d %H:%M:%S')
print(time)
However, the output is wrong as it is not converting correctly: 但是,输出错误,因为它无法正确转换:
2001-01-01 00:00:11
2001-01-01 00:00:00
2001-01-01 00:00:01
2001-01-01 00:00:02
2001-01-01 00:00:03
2001-01-01 00:00:04
2001-01-01 00:00:05
2001-01-01 00:00:06
When replacing 't' with an epoch time: 将“ t”替换为新纪元时:
import datetime
with open("user1_nsdate.csv",'r') as f:
for row in f:
for t, val in enumerate(row):
time = datetime.datetime.fromtimestamp(527131607.9 + 978307200).strftime('%Y-%m-%d %H:%M:%S')
print(time)
output: 输出:
2017-09-15 02:26:47
2017-09-15 02:26:47
2017-09-15 02:26:47
2017-09-15 02:26:47
2017-09-15 02:26:47
But i need it to iterate over every row in the first column of the csv file 但我需要它遍历csv文件第一列中的每一行
Change your strftime
to .strftime("%d-%m-%y %H:%M:%S")
将您的
strftime
更改为.strftime("%d-%m-%y %H:%M:%S")
>>> time = datetime.fromtimestamp(527131607.9+978307200).strftime("%d-%m-%y %H:%M:%S")
>>> print(time)
15-09-17 02:26:47
There's info on all the format codes here: 这里有所有格式代码的信息:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
Updated after your comment: 发表评论后更新:
If your file looks like this. 如果您的文件看起来像这样。
527131607.9
527131127.1
525123123.9
...
You could do something similar to this: 您可以执行以下操作:
with open('test.csv', 'r') as f:
for row in f:
time = datetime.fromtimestamp(float(row)+978307200).strftime("%d-%m-%y %H:%M:%S")
print(time)
It's a very simple file. 这是一个非常简单的文件。 I don't think we need to use the built in csv module.
我认为我们不需要使用内置的csv模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.