- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我用 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
(从 .gov 网站提取的公共(public)数据)
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
最佳答案
dictReader 是一个迭代器,一旦它读完 CSV 文件,它就会耗尽:进一步的迭代将不会做任何事情。
解决此问题的方法是在 writeTypes 中创建一个新的字典列表,这样您就可以在此处而不是在原始列表中分配值。然后您可以返回该列表,并在 main 函数中对其进行迭代。
关于Python newb : What's preventing this function from printing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24290698/
根据我读过和遵循的一些示例,我认为这会非常简单......但是 Ps 对我大喊大叫。有什么想法这有什么问题吗?我是在 PS 窗口而不是 ISE 中执行此操作。 $name = "SharePoint
我在理解选择器和索引在 JQuery 中的工作方式时遇到一些问题。我希望编写一些基于类属性操作 html 元素的函数,我意识到当我尝试使用 index() 方法或 prevAll().length 来
我最近在练习 React,所以通过 react-create-app 创建了一个简单的应用程序,并且没有编辑任何配置。 我想做的只是渲染 fullcalendar,因此安装了来自 fullcalend
我是一个 C 语言新手,正在努力解决作业中的 C 语言问题。我需要创建一个简单的程序,将结构类型写入文件,然后读取文件以恢复先前插入的数据。我让这两个部分工作。 还有另一个功能是必需的,它基本上要求用
Closed. This question is off-topic。它当前不接受答案。
我已经想出了如何做我需要做的大部分事情。我意识到 for 循环中使用的变量在循环外是不可访问的,但我需要显示用户输入的整数的总和。所以, 第 1 步:要求用户输入整数个数。 第 2 步:获取每个整数。
我正在努力掌握指针及其强大之处,同时更好地理解 C++。我不知道为什么这不会编译。请告诉我哪里出了问题?我试图在创建类的实例时初始化指针。如果我尝试使用普通的 int 它工作正常但是当我尝试使用指针设
我正在尝试让第三方音频库( STK )在 Xcode 中工作。除了标准 .h 文件之外,许多实现文件还包含一个名为 SKINI.msg 的文件。 SKINI.msg 与所有头文件位于同一目录中。头文件
我是 JS 的新手,尤其是 Typescript。我正在使用 npm 6.2.0 和 typescript 3.0.1。我正在尝试导入“文件保护程序”模块并使用它。它的 index.d.ts 看起来像
感谢强大的 Johan 的帮助。但我无法让我的数据库更新。如果你有时间请查看我的两个文件并告诉我 F 有什么问题 我的 hire_staff.php function bye(){ a
嘿,我正在尝试让它工作,但我不是 C# 程序员。我得到这段代码来向你展示我想做什么。但原因是它不起作用。错误:CS1519:无效的 token ... if(Request.ServerV
我写了我的第一个 ajax 脚本,经过一整夜的努力,它在我的本地网络服务器上完美运行,但是当我上传到我的网络主机时却什么也没做。我怀疑是因为我在使用 getjson 并且 get 在 Web 主机上被
如果 EQ_Type!='ENGINE',是否可以让这个 if 语句不打印列?输出中的空列让我很困扰。 select if(EQUIPMENT.EQ_Type='ENGINE',ENGINE.Capa
pyparsing 的作者 Paul McGuire 是 kind enough to help a lot with a problem I'm trying to solve .我们落后一码,但我
背景 我用 python 的工作不多,但我想用它为我生成一些重复的 XML。现在,我只想解析 CSV,然后将这些值传递到 XML 节中。 有一个问题:在编写 XML 之前,我需要重写一些 CSV。我有
我正在尝试了解如何在我的 C++ 项目中使用非标准库。我有几个问题。 假设我想使用 POCO 库。所以我下载了它并使用 make(静态构建)构建它。现在我有一堆 .o 文件和 .h 文件。不同目录下有
我是第一次尝试学习 Android Studio/Java。我熟悉 c#、vb.net、javascript 和其他一些语言。 video: Android Studio for Beginners我
我一直在寻求这方面的帮助,但我觉得我总是听到的答案总是把责任推给用户。这个问题几乎感觉像是 git 本身的错误。为什么 git 甚至允许这种情况发生? 情况是这样的。在 merge 冲突期间,没有冲突
这里是新手...刚开始接触 rspec,有以下问题: describe Song do before do @song = Song.new end describe 'titl
试图让我的头脑绕过这个 MySQL 胡言乱语,哈哈 - 现在我通常不会寻求帮助,但在这个问题上我认为我确实需要一推。 我已经搜索了这个问题的答案,但是解决方案都是基于 fetch_array 命令的询
我是一名优秀的程序员,十分优秀!