背景
我在python上工作不多,但是我想用它为我生成一些重复的XML。现在,我只想解析CSV,然后将这些值传递到XML节中。
有一个陷阱:在编写XML之前,我需要重写一些CSV。我有一些if
语句可以帮我解决这个问题,因此我决定通过将其移至单独的函数来减少混乱。
这是我的问题出现的地方。我的writeTypes
函数似乎可以正常工作,但是当我返回重写的csvDict实例时,我将无法再打印值。
显然,我缺少了一些东西,可能很简单-但是呢?带有以下注释的脚本。
脚本
import csv
def parseCSV(vals):
# read the csv
dictReader = csv.DictReader(open(vals, 'rb'), fieldnames=['name', 'type', 'nullable', 'default', 'description', '#'], delimiter=',', quotechar='"')
# some repetitive xml; I will finish this portion later...
stanza = '''
<var name="{0}" precision="1" scale="None" type="{1}">
<label>{2}</label>
<definition><![CDATA[@{3}({4})]]></definition>
</var>'''
# a function that simply writes new values to dictionary entries
writeTypes(dictReader)
# I'm confused here - nothing is printed to the console.
# If i comment my 'writeTypes function, prints as expected
for i in dictReader:
print i
print i['type']
# function to rewrite 'types' key in dictionary set
def writeTypes(d):
for i in d:
if i['type'] == 'text':
i['type'] = 't'
elif i['type'] == 'boolean':
i['type'] = 'l'
elif i['type'] == 'double precision':
i['type'] = 'd'
elif i['type'] == 'integer':
i['type'] = 'i'
else:
i['type'] = i['type']
# unsurprisingly, this function does seem to print the correct values
print i
# it seems as though there's something wrong with this return statement...
return d
CSV范例
(从.gov网站提取的公共数据)
Name,Type,Nullable,Default,Description,#
control,text,true,,,1,false
flagship,boolean,true,,,1,false
groupid,text,true,,,1,false
hbcu,text,true,,,1,false
hsi,text,true,,,1,false
iclevel,text,true,,,1,false
landgrnt,text,true,,,1,false
matched_n_00_10_11,boolean,true,,,1,false
matched_n_05_10_6,boolean,true,,,1,false
matched_n_87_10_24,boolean,true,,,1,false
name,text,true,,,1,false
name_short,text,true,,,1,false
school,text,true,,,1,false
sector,text,true,,,1,false
sector_revised,text,true,,,1,false
top_50,boolean,true,,,1,false
virginia,boolean,true,,,1,false
最佳答案
@Jefftopia,问题在于您第一次将dictReader
用作迭代器会“消耗”整个文件,因此,当您尝试第二次遍历该文件时,没有什么可读取的。
当您这样做时...
# a function that simply writes new values to dictionary entries
writeTypes(dictReader)
... writeTypes函数通过
dictReader
遍历CSV文件的行:def writeTypes(d):
for i in d:
...
然后,您从该函数返回并尝试再次遍历
dictReader
。问题在于dictReader
现在已经没有数据可以从基础文件中读取,因为它已经遍历了整个过程!# I'm confused here - nothing is printed to the console.
# If i comment my 'writeTypes function, prints as expected
for i in dictReader:
print i
print i['type']
在Python中将
file
对象或大多数类似的对象用作迭代器时,迭代器“使用”文件。通常,没有办法可靠地读取文件状对象,然后返回到开头再次读取它(考虑到网络套接字只能流一次数据的情况)。在这种情况下,您可以在第二次通过数据之前简单地再次打开文件。 (还有更多的kludge-y解决方案,但我不会显示它们。)
# reopen the file in order to read through it a second time
dictReader = csv.DictReader(open(vals, 'rb'), fieldnames=['name', 'type', 'nullable', 'default', 'description', '#'], delimiter=',', quotechar='"')
for i in dictReader:
print i
print i['type']
多次通过文件处理有时可以大大简化这样的代码,尽管它也会损害大型文件的性能。在这种特殊情况下,一次完成所有操作将很简单。您可以稍微重写一下代码,以便在遍历行时收集
type
字段。关于python - Python newb:是什么导致此函数无法打印?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24290698/