为什么这个程序可以在Linux Python Shell 上运行而不能在Windows 上运行?

发布于 2024-12-11 09:57:45 字数 1507 浏览 0 评论 0原文

我在 Linux 上使用 Python 2.6.2 运行这个程序,它运行得很好,返回了十进制值,但是当我在 Windows 上的 Python 2.7.2 上运行它时,它不起作用,只是给出了一段空白,然后出现内存错误但我不明白为什么..我需要它在 Windows 上运行,它是一个计算股本 (ROE) 的程序。谢谢。

运行该程序所需的 CSV 文件位于此处 。 。

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)

I ran this program on Linux with Python 2.6.2 and it ran fine returning me with decimal values but when I run it on Python 2.7.2 on Windows it does not work and just gives a blank space for a while and then a memory error but I can't figure out why..I need it to run on Windows its a program to calculate stock equity (ROE). Thanks.

The CSV file needed to run the program is 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光沙漏 2024-12-18 09:57:45

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

You have stuffed up the indentation of your a+=1 line, possibly because of (mis)use of tabs in your source file. As displayed here on SO, a will never be incremented and so the loop will never be exited.

謸气贵蔟 2024-12-18 09:57:45

我对您的脚本进行了一些修改。
它在 Windows 的 Python 2.7 上运行良好。
这是代码:

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.2944854035689894、 0.1283120464917753, 0.2573271287452037]

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

I added a few modifications to your script.
It works fine on Python 2.7 for Windows.
Here's the code:

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)

The output was :
Average equity is [2071.11, 3505.7650000000003, 3325.3650000000002, 3273.6400000000003, 3398.375, 4187.76, 5197.549999999999]

ROE method 1 is [0.12812453225565035, 0.15742791098732495, 0.23651124740462906, 0.2532005689900426, 0.2944854035689894, 0.1283120464917753, 0.2573271287452037]

ROE method 2 is [0.12812453225565038, 0.17126298080734237, 0.21680660107401206, 0.2613058810726202, 0.29811440335236883, 0.1466466034500227, 0.2814118207249569]

够运 2024-12-18 09:57:45

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

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

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

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

It helps to know where it's blowing up. You should try the Python debug module if the error isn't enough information. When executing Python e.g. "python ./script.py", try "python -m pdb ./script.py" and step through to see how far it gets. (type help for more info) If you're getting no feedback before the memory error.

How long is the csv file? (we'll assume you're using the same data on both platforms) There are 2 loops that might matter. When looping through sbuxfile (e.g. "for row in sbuxfile:"), you don't have a stop, but the 2nd loop (e.g. "while a < 8:") you only process 7 rows. Usually you get a memory error in cases were repeated data or large data is iterated over and maintained. If the csv is giant and you only wanted to read the first 7 lines, then break out of the csv read loop.

Also, I don't know what the purpose of equating the row values in your csv read loop is accomplishing. If you wanted to stuff the csv fields into array values, you should do so. e.g. row8 = row

I won't bother to ask if the ram memory size is different between the platforms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文