简体   繁体   English

在Python中,如何在通过循环创建的python列表中获取条目的值

[英]In Python, how do I grab the value of an entry in a python list created through a loop

I am writing Python code to pull data from a csv file and format it into geoJSON. 我正在编写Python代码以从csv文件中提取数据并将其格式化为geoJSON。 I want to also grab single entries from the data to include in the properties of the geoJSON. 我还想从数据中获取单个条目以包含在geoJSON的属性中。 For example, I convert a column of csv timestamps into geoJSON, and I also want to pull out the first and last timestamps in that list for the properties below: 例如,我将一列csv时间戳转换为geoJSON,并且我还想为该属性提取该列表中的第一个和最后一个时间戳:

   "properties": {
        "species": "racoon",
        "id": "12345",
        "first transmitted": "Aug 12, 2014",
        "last transmitted": "Nov 1, 2015"
}

I have written Python code that converts a column of timestamps from the csv to the geoJSON format: 我编写了Python代码,将一列时间戳从csv转换为geoJSON格式:

# loop through the csv by row skipping the first
iter = 0

timevals = ""
for row in rawData:
    iter += 1
    if iter >= 2 and itemid == row[1]:
        rawtime = row[4]
        rawtime = rawtime.replace("T", " ")
        rawtime = rawtime.replace("Z", "")
        timevals += template_time % int( time.mktime(time.strptime(rawtime, '%Y-%m-%d %H:%M:%S') ))

This gives me the list of timestamps from the csv file formatted for my geoJSON file. 这为我提供了为我的geoJSON文件格式化的csv文件中的时间戳列表。 Now I want to grab the first and last timestamp in that list. 现在,我想获取该列表中的第一个和最后一个时间戳。 I altered it like this in order to try to grab the first: 我这样修改了它,以尝试抓住第一个:

# loop through the csv by row skipping the first
iter = 0

timevals = ""
for row in rawData:
    iter += 1
    if iter >= 2 and itemid == row[1]:
        rawtime = row[4]
        rawtime = rawtime.replace("T", " ")
        rawtime = rawtime.replace("Z", "")
        timeval = int( time.mktime(time.strptime(rawtime, '%Y-%m-%d %H:%M:%S') ))
        timevals += template_time % timeval
    startTime = timeval.iloc[0]
    print startTime

My error is: 我的错误是:

startTime = timeval.iloc[0]
AttributeError: 'int' object has no attribute 'iloc'

What is the way to write this? 写这个的方式是什么?

EDIT I got the startTime by initializing 编辑我通过初始化获得了startTime

startTime = ""

and then writing 然后写

if startTime == "":
    startTime = rawtime

so now I have the following, plus the endTime: 所以现在我有了以下内容以及endTime:

# loop through the csv by row skipping the first
iter = 0
startTime = ""

for row in rawData:
    iter += 1
    if iter >= 2 and itemid == row[1]:  # itemid
        rawtime = row[4]
        rawtime = rawtime.replace("T", " ")
        rawtime = rawtime.replace("Z", "")

        if startTime == "":
            startTime = rawtime

        endTime = rawtime

This ends up producing the first and final values, startTime and endTime, though its a bit of a trick and I was wondering if there was something more pythonic. 最终产生了第一个和最后一个值,startTime和endTime,尽管有点技巧,但我想知道是否还有更多的pythonic。

Thanks for the clarification edit. 感谢您的澄清编辑。 Can you easily hold the data in memory? 您可以轻松地将数据保存在内存中吗? If so, you have a simple path: define a function for the conversion, then apply that only to the first and last items. 如果是这样,则您有一条简单的路径:为转换定义一个函数,然后仅将其应用于第一项和最后一项。

I'm assuming that you're skipping the first line because it's header information. 我假设您正在跳过第一行,因为它是标题信息。 If so, then you don't really need to check for it explicitly; 如果是这样,那么您就不需要显式检查它了; its 2nd entry won't match itemid, anyway. 无论如何,其第二个条目将与itemid不匹配。

def convert_time(rawtime):
    rawtime = row[4]
    rawtime = rawtime.replace("T", " ")
    rawtime = rawtime.replace("Z", "")

row_stream = [row in rawData if row[1] == itemid]
startTime = convert_time(row_stream[0])
endTime = convert_time(row_stream[-1])

If you have too much data for this to work, then trade the list comprehension for a generator, and let the Python run-time take care of the optimizations: 如果您有太多的数据无法使用,请使用列表理解权交换一个生成器,然后让Python运行时负责优化:

row_stream = (row in rawData if row[1] == itemid)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 如何将用户条目添加到列表中的新值? - Python how do I add user entry to a new value on a list? Python脚本读取列表并获取反向DNS条目 - Python script to read through a list and grab reverse DNS entry 如何在python中使用for循环搜索文件列表 - How do I search through a list of files using a for loop in python 如何循环遍历python中的大型嵌套列表? - How do I loop through a large nested list in python? 如何在Python中遍历列表以确保没有重复? - How do I loop through a list in Python to make sure there are no repeats? 如何在python中遍历字节的每个可能值? - How do I loop through every possible value of a byte in python? 如何遍历字典列表,如何从URL(值)中获取图像,并以键作为文件名将其写入磁盘? - How do I loop through a list of dictionaries, grab the image from the URL(value) and write it disk with the key as the file name? 我如何遍历以逗号分隔的字符串中的每个条目,并使用 Python 将其作为单独的输入包含在内 - How do i loop through each entry in a string delimited by a comma and include it as a separate input using Python 如何使用Python获取字母数字列表中的最高值? - How can I grab the highest value in alphanumeric list using Python? 我已经在python中创建了一个列表,但是如何在javascript中遍历该列表呢? - I've created a list in python but how do I iterate through this list in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM