Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从混合列表中提取整数_Python_Xlrd_Xlwt - Fatal编程技术网

Python 从混合列表中提取整数

Python 从混合列表中提取整数,python,xlrd,xlwt,Python,Xlrd,Xlwt,我试图从csv文件中读取,提取一些值并将其复制到现有文件中 我的文件中的行如下所示: row = ['success', '9358', 'MC', '9363.0', 'MC', '1.001', '9363.0', 'MC', '1.001'] 我尝试提取整数值9358,并尝试了以下操作: int(行[1]) map(int,行[1]) map(int,第['1'行]) int(第[1]行)。替换(“,”) int(行[1])的错误是: Traceback (most recent cal

我试图从csv文件中读取,提取一些值并将其复制到现有文件中

我的文件中的行如下所示:

row = ['success', '9358', 'MC', '9363.0', 'MC', '1.001', '9363.0', 'MC', '1.001']
我尝试提取整数值9358,并尝试了以下操作:

  • int(行[1])
  • map(int,行[1])
  • map(int,第['1'行])
  • int(第[1]行)。替换(“,”)
  • int(行[1])
    的错误是:

    Traceback (most recent call last):
      File "upv_c.py", line 26, in
        num1 = int(row[1])
          ValueError: invalid literal for int() with base 10: ''
    

    但每一个都会带来一些错误。 你知道我该怎么做吗

    我的代码如下:

    #!/home/utils/Python-2.7/bin/python2.7
    import csv
    import xlwt
    import xlrd
    from xlutils.copy import copy
    book = xlrd.open_workbook('reg_test.xls')
    wb = copy(book) # a writable copy 
    w_sheet = wb.get_sheet(0)
    with open('results2.csv', 'r') as f:
        reader = csv.reader(f)
        next(reader, None)
        next(reader, None)
        next(reader, None)
        next(reader, None)
        i = 1
        for row in reader:
            num = map(int, row[1])
            w_sheet.write(i, 2, num)
            i += 1
    wb.save('reg_test.xls')
    

    你到底犯了什么错误?如果试图转换实际数字字符串,则不应出现错误。尝试使用Try-catch块查看发生了什么

    try:
    for row in reader:
        num = map(int, row[5])
        w_sheet.write(i, 2, num)
        i += 1
    except Exception, e:
        print 'Unable to convert', row[5]
    
    您是否尝试过:

    if row.isnumeric():
        my_var = int(row)
    

    这样,在将字符串参数传递给
    int()

    之前,您可以确保它实际上是数字参数。我看到您的数字被括在“”中,如“123” 当您试图在python中读取时,它在变量中被读取为“'123'”

    temp=row[1].replace("'","")
    
    然后


    “但是每一个都会抛出一些错误。”好吧,那你为什么要用
    num1=int(第[6]行]
    而不是
    num1=int(第[1]行]
    ?对不起!真正的名单太长,无法发布。我确保索引没有错误。问题是索引6处只有一个空字符串。没有!那是个错误。这是第[1]行。无法转换回溯(最后一次调用):文件“upv_c.py”,第33行,打印“无法转换”,第[5]行
    num = int(temp)