English 中文(简体)
在 Python 中如何从有时缺少数据的列表中指定变量
原标题:In Python how to assign variables from list that is sometimes missing data
  • 时间:2024-05-22 20:06:45
  •  标签:
  • python
  • list

我的代码读取来自 NOAA s 浮标服务的数据。 URL 以. txt 格式, 我的代码然后根据列表中的变量位置来指定变量。 我指定的变量“ 线” 列表通常包含以下信息 :

[ Station CAMM2 , "38° 34.4  N  76° 4.1  W",   ,  3:00 pm EDT ,  1900 GMT 05/22/24 ,  Wind: SSW (210°), 9.9 kt ,  Gust: 15.0 kt ,  Pres: 29.96 falling ,  Air Temp: 82.8 °F ,  Water Temp: 68.5 °F ,   ]

我想指派像这样的变量 :

when = lines[3]
wind = lines[5]
gust = lines[6]
pressure = lines[7]
air_temp = lines[8]
water_temp = lines[9]

然而,有时在文本种子中缺少一个或多个元素。 例如, 种子中将缺少 < code> air_ temp , 因此 < code> water_temp = lines[9] 将导致索引外错误。 我知道这是基本错误, 但我如何从列表中指定变量, 而列表中总是有相同数量的元素?


在列表上循环, 并根据元素内容指定正确的变量 :

when = lines[3]
# initialize all the variables with default values
wind = gust = pressure = air_temp = water_temp =   
for item in lines:
    if item.startswith( Wind: ):
        wind = item
    elif item.startswith( Gust: ):
        gust = item
    # and so on


您可以在字典中存储它们, 在每个元素的起始处使用字符串作为字典键 。

dd = dict([t.split( :  ) for t in lines[5:] if t !=   ])


{ Wind :  SSW (210°), 9.9 kt ,
  Gust :  15.0 kt ,
  Pres :  29.96 falling ,
  Air Temp :  82.8 °F ,
  Water Temp :  68.5 °F }

然后您可以引用每个字典条目获取信息 。

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...