English 中文(简体)
空间分辨率数据
原标题:space separated data into a dict
  • 时间:2012-05-15 13:24:47
  •  标签:
  • python

I m试图取得以下产出:2_YEAR_912828SR2_20120514.dat 1337045684,并创建名词典,名称为关键值和日期

#get file attributes
datadict = {}

file_list_attr = sftp.listdir_attr()
for f in (file_list_attr): 
   fname = f.filename
   fdate = f.st_mtime

   print fname, fdate

2_YEAR_912828SR2_20120514.dat 1337045684

最佳回答

这里是一个工作例子,也关注数据转换(时间范围——日期);

from datetime import date

with open("file_with_data.txt", "r") as fl:
    datadict = dict([ (file_name, date.fromtimestamp(float(timestamp))
      for line in fl.readlines() for file_name, timestamp in line.split()])
问题回答

如果您有<代码>f,作为具有这些特性的物体:

for f in (file_list_attr):
    datadict[f.filename] = f.st_mtime

That s taking your code as it is. But I think you have f as a string, so you would have to split it in two:

for f in (file_list_attr):
    filename, st_mtime = f.split(   , 2)
    datadict[filename] = st_mtime
dict((f.filename, f.st_mtime) for f in file_list_attr)

或 (didn t了解q)与空间有关的线路:

dict(f.split(   , 2) for f in file_list_attr)

Not knowing exactly what your code is.

data = {}
for file in file_list:
    data[f.filename] = f.st_mtime

Is not clear what you need, if you want to process the output of your code to create a dict you could make something like this:

output = """2_YEAR_912828SR2_20120514.dat 1337045684
2_YEAR_912828SR2_20120515.dat 1337045684
2_YEAR_912828SR2_20120516.dat 1337045684"""

result_dict = {}
for l in output.splitlines():
    k, v = l.split()
    result_dict[k] = v




相关问题
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 ]="...

热门标签