我应该做的一件事就是从我给出的文件中的数据中找出平均通货膨胀率。它会为year
,interest
和inflation
提供下面的所有数字。看起来像这样。
year interest inflation
1900 4.61 8.1
这种情况相当进一步,直到2008年。由于德国编码器的帮助,我在一些帮助下取得了一些进展。我仍然被困住了。
到目前为止,这是代码的样子。
def myTest(file):
with open ('filename', 'r') as f:
inflation = []
header = 1
for line in f:
if header !=1:
infl = line.split(",")[2]
inflation.append(float(infl))
header += 1
avgInflation = sum(inflation)/len(inflation)
return avgInflation
我认为,问题在于年度利息通胀导致问题。所以在帮助下我添加了代码,但我仍然收到错误。它表示除以零错误,第11行。对我现在应该做什么的任何想法?
实际文件名是Inflation.csv。我自己创建了一个简单的程序,它在解释器中打印出来,用逗号分隔它,因此我做了(“,”)
答案 0 :(得分:0)
由于header += 1
条件header != 1
且header
的初始值为1
,因此您无法满足追加标头的条件。因此,您循环遍历数组并且什么都不做。在您计算平均值的方法结束时,您的inflation
列表仍然为空,因此len(inflation)
会解析为0
。
此外,您正在错误地读取文件中的行。我想你想用f.readlines()
来读它们作为行?
def myTest(file):
with open ('filename', 'r') as f:
lines = f.readlines() <--- 2
inflation = []
header = 1
for line in lines:
if header !=1:
infl = line.split(",")[2]
inflation.append(float(infl))
header += 1 <--- 1
avgInflation = sum(inflation)/len(inflation)
return avgInflation
答案 1 :(得分:0)
您正在初始化header = 1
,并在检查infl
是否不是inflation
之后初始化header
和avgInflation = sum(inflation)/len(inflation)
的值,因此,avgInflation = sum(0/len(0))
是有效的成为:
header = 0
在分区中使用之前,请务必检查通胀是否有值。也许你打算初始化@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "SPLITCHAR=-" & rem // (a single character to split the file names)
set "SEARCHSTR=_" & rem // (a certain string to be replaced by another)
set "REPLACSTR= " & rem // (a string to replace all found search strings)
set "OVERWRITE=" & rem // (set to non-empty value to force overwriting)
rem // Get file location and pattern from command line arguments:
set "LOCATION=%~1" & rem // (directory to move the processed files into)
set "PATTERNS=%~2" & rem // (file pattern; match all files if empty)
rem /* Prepare overwrite flag (if defined, set to character forbidden
rem in file names; this affects later check for file existence): */
if defined OVERWRITE set "OVERWRITE=|"
rem // Continue only if target location is given:
if defined LOCATION (
rem // Create target location (surpress error if it already exists):
2> nul md "%LOCATION%"
rem /* Loop through all files matching the given pattern
rem in the current working directory: */
for /F "eol=| delims=" %%F in ('dir /B "%PATTERNS%"') do (
rem // Process each file in a sub-routine:
call :PROCESS "%%F" "%LOCATION%" "%SPLITCHAR%" "%SEARCHSTR%" "%REPLACSTR%"
)
)
endlocal
exit /B
:PROCESS
rem // Retrieve first argument of sub-routine:
set "FILE=%~1"
rem // Split name at (first) split character and get portion in front:
for /F "delims=%~3" %%E in ("%~1") do (
rem // Append a split character to partial name:
set "FOLDER=%%E%~3"
)
setlocal EnableDelayedExpansion
rem // Right-trim partial name:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4%~3=!"
set "FOLDER=!FOLDER:%~3=!"
rem /* Check whether partial name is not empty
rem (could happen if name began with split character): */
if defined FOLDER (
rem // Replace every search string with another:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4=%~5!"
rem // Create sub-directory (surpress error if it already exists):
2> nul md "%~2\!FOLDER!"
rem /* Check if target file already exists; if overwrite flag is
rem set (to an invalid character), the target cannot exist: */
if not exist "%~2\!FOLDER!\!FILE!%OVERWRITE%" (
rem // Move file finally (surpress `1 file(s) moved.` message):
1> nul move /Y "!FILE!" "%~2\!FOLDER!"
)
)
endlocal
exit /B
答案 2 :(得分:0)
这个怎么样?
def myTest(file):
with open ('filename', 'r') as f:
inflation = []
header = 1
for line in f:
if header == 1:
header += 1
continue
else:
infl = line.split(",")[2]
inflation.append(float(infl))
avgInflation = sum(inflation)/len(inflation)
return avgInflation
我认为这应该可以解决问题。
答案 3 :(得分:-1)
我看到你在for循环之外启动变量header
为1。
在for循环中,检查header
是否不等于1,如果等于1,则不执行任何操作。
for循环永远不会进入if header !=1:
它只是迭代你的整个文件,并在没有做任何事情的情况下退出for循环。
在第11行中,len(inflation)
为0,因为您从不向它添加任何内容。
您应该更改条件循环或在代码中的某处更改变量header
。
def myTest(file):
with open ('filename', 'r') as f:
inflation = []
header = 1
for line in f:
if header !=1: //Never goes inside
infl = line.split(",")[2]
inflation.append(float(infl))
header += 1
avgInflation = sum(inflation)/len(inflation) //Divison by zero
return avgInflation