为什么这个程序在linuxpythonshell上运行而不在Windows上运行?

2024-06-02 10:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python2.6.2在Linux上运行这个程序,它很好地返回了十进制值,但当我在Windows上的Python2.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)

Tags: csv程序forcountfloatrowroerow3
3条回答

您已经将a+=1行的缩进塞满了,可能是因为源文件中(错误地)使用了制表符。如这里所示,a永远不会递增,因此循环永远不会退出。在

我对你的剧本做了一些修改。 它在python2.7forwindows上运行良好。 代码如下:

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.113505.7650000000003325365000000000023273640000000033398.375 4187.765197.54999999999]

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

ROE方法2为[0.12812453225565038,0.17126298080734237,0.21680660107401206,0.26130588107726202,0.29811440335236883,0.14664666034500227,0.2814118207249569]

知道它在哪里爆炸是有帮助的。如果错误信息不足,您应该尝试Python调试模块。当执行Python时,例如“Python”/脚本.py,尝试“python-m pdb”/脚本.py“一步一步来看看能走多远。(键入help以获取更多信息)如果在内存错误之前没有收到任何反馈。在

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

另外,我不知道在csv read循环中相等行值的目的是什么。如果您想将csv字段填充到数组值中,您应该这样做。e、 g.row8=世界其他地区

我不想问,不同平台之间的ram内存大小是否不同。在

相关问题 更多 >