为什么这个程序在Linux Python Shell上运行但不在Windows上运行?

时间:2011-10-24 19:49:16

标签: python windows linux shell python-idle

我在Linux上使用Python 2.6.2运行了这个程序,它运行良好,返回我的十进制值,但当我在Windows上运行它在Python 2.7.2上它不起作用,只是给了一段时间的空白,然后内存错误,但我无法弄清楚为什么..我需要它在Windows上运行它的程序来计算股票权益(ROE)。感谢。

运行程序所需的CSV文件是here

import csv

csvname = raw_input("Enter csv name: ")

sbuxfile = csv.reader(open(csvname), delimiter=',', quotechar='|')

# List of Data
row5, row8, row3, row7, avgequity, roe1, roe2 = ([] for i in range(7))

count = 0
# Grab data and numerical values from CSV.
for row in sbuxfile:
  count += 1
  if count == 8:     
     row8 = row
  elif count == 5:   
     row5 = row 
  elif count == 3:   
     row3 = row 
  elif count == 7:   
     row7 = row


a = 1

# Perform calculations for average equity and ROE.
while a < 8 :
   if a == 1:
     avgequity.append(round(float(row8[a]),2))
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / float(row8[a]))) 
   else:    
     avgequity.append(round(float((row8[a]),2) + float(row8[a-1]))/2)
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / ((float(row8[a]) + float(row8[a-1]))/2)))     
     a+=1 

print "\nAverage equity is " + str(avgequity) + "\n"
print "ROE method 1 is " + str(roe1) + "\n"
print "ROE method 2 is " + str(roe2)

3 个答案:

答案 0 :(得分:1)

您填写了a+=1行的缩进,可能是因为(错误)使用了源文件中的制表符。如此处显示的那样,a永远不会递增,因此永远不会退出循环。

答案 1 :(得分:0)

它有助于了解它在哪里爆炸。如果错误信息不足,您应该尝试使用Python调试模块。执行Python时,例如“python ./script.py”,尝试“python -m pdb ./script.py”并逐步查看它到底有多远。 (输入帮助以获取更多信息)如果在内存错误之前没有得到任何反馈。

csv文件有多长? (我们假设您在两个平台上都使用相同的数据)有两个循环可能很重要。当循环遍历sbuxfile时(例如“for sbuxfile中的row:”),你没有停止,但第二个循环(例如“while a&lt; 8:”)你只处理7行。通常在重复数据或大数据被迭代和维护的情况下会出现内存错误。如果csv是巨型的,你只想读取前7行,那么就要打破csv读取循环。

另外,我不知道在csv读取循环中等于行值的目的是什么。如果要将csv字段填充到数组值中,则应该这样做。例如row8 = row

我不会费心去问平台之间的ram内存大小是否不同。

答案 2 :(得分:0)

我在您的脚本中添加了一些修改。 它适用于Python 2.7 for Windows。 这是代码:

import csv

csvname = raw_input("Enter csv name: ")

sbuxfile = csv.reader(open(csvname), delimiter=',', quotechar='|')
# List of Data
row5, row8, row3, row7, avgequity, roe1, roe2 = ([] for i in range(7))

count = 0
# Grab data and numerical values from CSV.
for row in sbuxfile:
  count += 1
  if count == 8:     
     row8 = row
  elif count == 5:   
     row5 = row 
  elif count == 3:   
     row3 = row 
  elif count == 7:   
     row7 = row


a = 1

# Perform calculations for average equity and ROE.
while a < 8 :
   if a == 1:
     avgequity.append(round(float(row8[a]),2))
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / float(row8[a])))
     a+=1   #added this line
   else:    
     avgequity.append(round(float(row8[a]),2) + float(row8[a-1])/2) #rewrote this line as it had an error
     roe1.append(float(row5[a]) / float(row8[a]))
     roe2.append((float(row5[a]) / float(row3[a])) * (float(row3[a]) / float(row7[a])) * (float(row7[a]) / ((float(row8[a]) + float(row8[a-1]))/2)))     
     a+=1 

print "\nAverage equity is " + str(avgequity) + "\n"
print "ROE method 1 is " + str(roe1) + "\n"
print "ROE method 2 is " + str(roe2)

输出结果为: 平均权益为[2071.11,3505.7650000000003,3325.3650000000002,3273.6400000000003,3398.375,4187.76,5197.549999999999]

ROE方法1为[0.12812453225565035,0.15742791098732495,0.23651124740462906,0.2532005689900426,0.2938854035689894,0.1283120464917753,0.2573271287452037]

ROE方法2是[0.12812453225565038,0.17126298080734237,0.21680660107401206,0.2613058810726202,0.29811440335236883,0.1466466034500227,0.2814118207249569]