简体   繁体   English

迭代CSV列Python中行中的每个整数

[英]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.

相关问题 通过在 Python 中使用 pandas 迭代 csv 中特定列中的每个单元格 - Iterate each cell in a specific column in csv by using pandas in Python Python:遍历CSV的每一行,计数每一行中的令牌,创建一个具有原始CSV每行令牌数量的新CSV - Python: Iterate over every row of a CSV, count the tokens in every row, create a new CSV with the number of tokens for each row of the original CSV 将每一行转置为 python 中的一列(来自 csv 文件) - Transpose each row into a column in python (from a csv file) Python将csv列排序为整数 - python sort csv column as integer 迭代 CSV 中的每一列并对每一列应用加密 - Iterate each column in CSV and apply encryption to each column 遍历CSV的每一行,并使用Python-Pandas写入文件夹中的单个JSON文件 - Iterate through each row of CSV and write into individual JSON files in a folder using Python-Pandas Python CSV阅读器-比较一列中的每一行和每一行 - Python CSV Reader - Compare Each Row with Each Other Row Within One Column 使用 Pandas、python 迭代行 csv 文件 - Iterate row csv file using pandas, python 从Python中的csv文件的特定行进行迭代 - Iterate from a certain row of a csv file in Python XML元素树Python遍历子级并将每个子级另存为CSV列 - XML Element Tree Python Iterate through child and save each subchild as CSV column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM