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