【发布时间】:2014-11-02 20:37:43
【问题描述】:
我的程序应该从用户那里获取输入并读取名称为 input 的文件。读取的文件被保存到一个名为投资组合的字典中,然后我所要做的就是将投资组合中的每一行排序为键和值。
这是我的代码。
portfolio = {}
portfolio = file_read() #Reads the file through a function
if file_empty(portfolio) == True or None: #nevermind this, it works
print "The file was not found."
else:
print "The file has successfully been loaded"
for line in portfolio:
elements = line.strip().split(",") #separate lists by comma
print elements[0] #using this to check
print elements[1] #if it works at all
所有这些都是打印第一行中的第一个字母,即 S。显然 elements[1] 应该是第二个字母,但 index 超出范围,请赐教可能出了什么问题。
谢谢。
【问题讨论】:
-
为什么你对两个不同的东西使用相同的可变投资组合名称???
-
你的 file_read 函数会以 dcitionary 的形式返回值吗??
-
请分享你的整个代码???
-
gist.github.com/anonymous/790100c7d8d99f50be90 @Hackaholic 别介意第 64-68 行,现在只尝试不同的东西
标签: python list python-2.7 dictionary