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

最佳答案

我对你的剧本做了一些修改。
它在Python 2.7for 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.64000000003、3398.375、4187.76、5197.5499999999]
净资产收益率方法1为[0.12812453225565035,0.157427910987332495,0.23651124740462906,0.2532005689900426,0.29448540356894,0.1283120464917753,0.2573271287452037]
ROE方法2为[0.12812453225565038,0.17126298080734237,0.21680660107401206,0.261305881076202,0.29811440335236883,0.14664666034500227,0.2814118207249569]

关于python - 为什么该程序在Linux Python Shell上运行却在Windows上不能运行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7881110/

10-11 02:44
查看更多