Python 存在整数时查找空字符串:ValueError:基数为10的int()的文本无效:'';

Python 存在整数时查找空字符串:ValueError:基数为10的int()的文本无效:'';,python,string,csv,Python,String,Csv,当我的CSV文件实际上包含一个整数时,我得到一个空字符串。您可以通过使用注释标记错误的位置来查看错误发生的位置。当我要求它在一行上打印1950,然后当我要求它将“1950”转换为int时,它说它不能将空字符串转换为int 请帮忙!很抱歉,这是一个基本的问题,我对这一切还不熟悉 代码: import Averages import csv def csv_info(filepath): """Takes in a CSV file that contains information on the

当我的CSV文件实际上包含一个整数时,我得到一个空字符串。您可以通过使用注释标记错误的位置来查看错误发生的位置。当我要求它在一行上打印1950,然后当我要求它将“1950”转换为int时,它说它不能将空字符串转换为int

请帮忙!很抱歉,这是一个基本的问题,我对这一切还不熟悉

代码:

import Averages
import csv

def csv_info(filepath):
"""Takes in a CSV file that contains information on the models used 
in the program in later functions. Outputs a dictionary of dictionaries, mapping each model to
its specific information."""

output = {}

for row in csv.DictReader(open(filepath)):
    output[row["Model"]] = row
return output



def rho_calc_interp(model, interp_y_n): 

    model = csv_info("/home/patold/Organized_Files.csv")[model]
    if 'MOHC' in model["Model"]:
        start = '12-'
        end = '11.nc'
    else:
        start = '01-'
        end = '12.nc'
    if 'woa' not in model["Filename"]: # If the data is not WOA data
        print (model["s_yr"]) # Prints 1950
        s_yr = int(model["s_yr"]) # Finds an empty string???
        e_yr = int(model["e_yr"])
        # More...

def tryAll():
    modelInfo = csv_info("/home/patold/Organized_Files.csv")
    models = []
    for i in range(len(modelInfo.keys())):
        models.append(list(modelInfo.keys())[i])
    for model in range(len(models)):
        x1 = Averages.rho_calc_interp(str(models[model]), "No")
    print ("FINAL: ")
    print (x1)

tryAll()
输出:

Program Finished.
1950
/home/patold/.conda/envs/cmip5/lib/python3.5/site-packages/scipy/io/netcdf.py:297: RuntimeWarning: Cannot close a netcdf_file opened with mmap=True, when netcdf_variables or arrays referring to its data still exist. All data arrays obtained from such files refer directly to data on disk, and must be copied before the file can be cleanly closed. (See netcdf_file docstring for more information on mmap.)
  ), category=RuntimeWarning)
/home/patold/.conda/envs/cmip5/lib/python3.5/site-packages/numpy/ma/core.py:4185: UserWarning: Warning: converting a masked element to nan.
  warnings.warn("Warning: converting a masked element to nan.")
FINAL: 
{'Densities': array([ nan,  nan,  nan, ...,  nan,  nan,  nan]), 'Depths': array([  5.00000000e+00,   1.50000000e+01,   2.50000000e+01, ...,
         5.16612988e+03,   5.49924512e+03,   5.83129443e+03])}

Traceback (most recent call last):

  File "<ipython-input-27-c1efed0bb6ff>", line 1, in <module>
    runfile('/home/patold/Spyder_Py_Files/Driver.py', wdir='/home/patold/Spyder_Py_Files')

  File "/home/patold/.conda/envs/cmip5/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "/home/patold/.conda/envs/cmip5/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/patold/Spyder_Py_Files/Driver.py", line 51, in <module>
    tryAll()

  File "/home/patold/Spyder_Py_Files/Driver.py", line 47, in tryAll
    x1 = Averages.rho_calc_interp(str(models[model]), "No")

  File "/home/patold/Spyder_Py_Files/Averages.py", line 183, in rho_calc_interp
    s_yr = int(model["s_yr"])

ValueError: invalid literal for int() with base 10: ''
程序已完成。
1950
/home/patold/.conda/envs/cmip5/lib/python3.5/site packages/scipy/io/netcdf.py:297:运行时警告:当引用其数据的netcdf_变量或数组仍然存在时,无法关闭使用mmap=True打开的netcdf_文件。从这些文件中获取的所有数据数组都直接引用磁盘上的数据,必须在文件完全关闭之前进行复制。(有关mmap的更多信息,请参阅netcdf_文件docstring。)
),类别=运行时警告)
/home/patold/.conda/envs/cmip5/lib/python3.5/site packages/numpy/ma/core.py:4185:UserWarning:Warning:将屏蔽元素转换为nan。
warnings.warn(“警告:将屏蔽元素转换为nan”)
最终:
{'densitions':数组([nan,nan,nan,…,nan,nan,nan]),'depth':数组([5.00000000e+00,1.50000000e+01,2.50000000e+01。。。,
5.16612988e+03、5.49924512e+03、5.83129443e+03]))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/home/patold/Spyder_Py_Files/Driver.Py',wdir='/home/patold/Spyder_Py_Files')
文件“/home/patold/.conda/envs/cmip5/lib/python3.5/site-packages/spyder/utils/site/site-customize.py”,第866行,在runfile中
execfile(文件名、命名空间)
文件“/home/patold/.conda/envs/cmip5/lib/python3.5/site-packages/spyder/utils/site/site-customize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“/home/patold/Spyder\u Py\u Files/Driver.Py”,第51行,在
tryAll()
文件“/home/patold/Spyder\u Py\u Files/Driver.Py”,第47行,tryAll格式
x1=平均值。rho_calc_interp(str(models[model]),“No”)
文件“/home/patold/Spyder\u Py\u Files/Averages.Py”,第183行,在rho\u calc\u interp中
s_yr=int(型号[“s_yr”])
ValueError:基数为10的int()的文本无效:“”
已解决:


我进入并删除了Excel中CSV文件中看似空白的单元格。保存了这个,现在可以使用了。显然,它是在一个空白行中读取的…

请修复代码的缩进。完成,对此表示抱歉。在错误发生之前,您的程序正在打印
FINAL:
,但在打印之后没有调用
rho\u calc\u interp
;您能解释一下吗?代码仍然没有正确缩进。您正在循环中调用rho\u calc\u interp。您看到的是成功调用的打印和后续调用的异常。您在随后的调用中看不到任何打印内容,因为它正在打印空字符串。请修复代码缩进。完成,对此感到抱歉。您的程序在错误发生之前打印出
FINAL:
,但在打印后没有对
rho\u calc\u interp
的调用;您能解释一下吗?代码仍然没有正确缩进。您正在循环中调用rho\u calc\u interp。您看到的是成功调用的打印和后续调用的异常。您在后续调用中看不到任何打印内容,因为它正在打印空字符串。