这是如何运作的?
所以我试图理解 matplotlib.mlab 中 csv2rec 的源文件。它用于获取 csv 文件并将数据解析为特定格式。所以它可能需要一个字符串'234'并将其转换为int。或者获取一个日期字符串并将其转换为 python 日期时间。
def get_converters(reader):
converters = None
for i, row in enumerate(reader):
if i==0:
converters = [mybool]*len(row)
if checkrows and i>checkrows:
break
#print i, len(names), len(row)
#print 'converters', zip(converters, row)
for j, (name, item) in enumerate(zip(names, row)):
func = converterd.get(j)
if func is None:
func = converterd.get(name)
if func is None:
#if not item.strip(): continue
func = converters[j]
if len(item.strip()):
func = get_func(name, item, func)
else:
# how should we handle custom converters and defaults?
func = with_default_value(func, None)
converters[j] = func
return converters
我对这个功能的问题是“转换器”。它以“无”开始。然后'func = converters[j]' j我知道是一个刚刚通过枚举创建的数字。因此它正在寻找由 j 索引的相应转换器项。但转换器中没有任何内容,因为它是“无”,对吗?除非python程序不必从上到下阅读?在这种情况下,我们从接下来的两行“if len(item.st....etc)”或“else:”部分获取 func。但是,我只是认为必须从上到下阅读。
我不知道其他事情是否重要,所以我只包含了整个功能。 converterd 是一个字典映射,我相信用户可以提供作为参数来自动查找转换器。 checkrows 只是用户在开始时作为参数提供的一个数字,用于检查有效性。默认情况下为“无”。我还是个初学者,所以仅供参考。 =)
谢谢大家。这个网站太有帮助了!
So I'm trying to comprehend the source file for csv2rec in matplotlib.mlab. It is used to take a csv file and parse the data into certain formats. So it may take a string '234' and convert it to int. or take a date string and make it into python datetimes.
def get_converters(reader):
converters = None
for i, row in enumerate(reader):
if i==0:
converters = [mybool]*len(row)
if checkrows and i>checkrows:
break
#print i, len(names), len(row)
#print 'converters', zip(converters, row)
for j, (name, item) in enumerate(zip(names, row)):
func = converterd.get(j)
if func is None:
func = converterd.get(name)
if func is None:
#if not item.strip(): continue
func = converters[j]
if len(item.strip()):
func = get_func(name, item, func)
else:
# how should we handle custom converters and defaults?
func = with_default_value(func, None)
converters[j] = func
return converters
My issue with this function is 'converters.' It starts off as None. Then later 'func = converters[j]' j I know is a number which is just created through enumeration. So it is looking for the corresponding converters item as indexed by j. But there is nothing in converters because it is None right? Unless python programs don't have to be read from top to bottom? In that case we get the func from the next two lines "if len(item.st....etc)" or from the 'else:' section. But, I just assumed it would have to be read from top to bottom.
I don't know if any of the other things are important so I just included the whole function. converterd is a dictionary mapping I believe that the user can provide as a parameter to find a converter automatically. checkrows is just a number provided by the user as a parameter in the beginning to check for validity. It is by default None. I'm still kind of a beginner, so just fyi. =)
Thanks everyone. This site is so helpful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
转换器在循环开始时再次设置
So 之后它就不再是 None 了。
Converters gets set again at the beginning of the loop with
So after that it's not None anymore.
除非我遗漏了什么,否则在第一次迭代中“i”为 0,因此执行以下操作:
并初始化“converters”
Unless I'm missing something, on the first iteration "i" is 0, so the following is executed:
and that initializes "converters"
首先,
为
转换器
设置初始值。这样,如果迭代没有发生(因为readers
可能为空),那么当函数返回converters
时,它将存在并具有值None.
如果发生对
readers
的迭代,则在第一次迭代中converters
会立即重置为更有意义的值(当i==0
>):First,
sets an initial value for
converters
. This way, if the iteration doesn't happen (becausereaders
might be empty) then when the function returnsconverters
it will exist and have the valueNone
.If the iteration over
readers
happens, thenconverters
is immediately reset to a more meaningful value in the first pass through the iteration (wheni==0
):