一元+的错误操作数类型:'str'

 劳允旭易文忠名 发布于 2023-02-10 13:18

我无法弄清楚我在使用Python 2.7编写的代码时遇到的问题.我正在将引用转换为int,但我一直在获得类型异常bad operand type for unary +: 'str'.有人可以帮忙吗?

import urllib2
import time
import datetime

stocksToPull = 'EBAY', 'AAPL'


def pullData(stock):
    try:
        print 'Currently pulling', stock
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + \
            stock + '/chartdata;type=quote;range=3y/csv'
        saveFileLine = stock + '.txt'

        try:
            readExistingData = open(saveFileLine, 'r').read()
            splitExisting = readExistingData.split('\n')
            mostRecentLine = splitExisting[-2]
            lastUnix = mostRecentLine.split(',')[0]
        except Exception, e:
            print str(e)
            time.sleep(1)
            lastUnix = 0

        saveFile = open(saveFileLine, 'a')
        sourceCode = urllib2.urlopen(urlToVisit).read()
        splitSource = sourceCode.split('\n')

        for eachLine in splitSource:
            if 'values' not in eachLine:
                splitLine = eachLine.split(',')
                if len(splitLine) == 6:
                    if int(splitLine[0]) > int(lastUnix):
                        lineToWrite = eachLine + '\n'
                        saveFile.write(lineToWrite)
        saveFile.close()

        print 'Pulled', + stock
        print 'Sleeping....'
        print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
        time.sleep(120)

    except Exception, e:
        print 'main loop', str(e)


for eachStock in stocksToPull:
    pullData(eachStock)

我打的操作异常bad operand type for unary +: 'str'当它到达if int(splitLine[0]) > int(lastUnix):,即使测试时,被比较的两个值打印出整数.任何人都可以给我一些反馈吗?谢谢!

这是异常响应:

Currently pulling EBAY
2013-12-21 11:32:40
Pulled main loop bad operand type for unary +: 'str'
Currently pulling AAPL
2013-12-21 11:32:41
Pulled main loop bad operand type for unary +: 'str'`

DSM.. 29

你说这if int(splitLine[0]) > int(lastUnix):是造成麻烦的,但你实际上没有表明任何暗示的东西.我认为这一行是问题所在:

print 'Pulled', + stock

你明白为什么这一行会导致错误信息?你也想要

>>> stock = "AAAA"
>>> print 'Pulled', stock
Pulled AAAA

要么

>>> print 'Pulled ' + stock
Pulled AAAA

>>> print 'Pulled', + stock
PulledTraceback (most recent call last):
  File "", line 1, in 
    print 'Pulled', + stock
TypeError: bad operand type for unary +: 'str'

你要求Python将+符号应用于一个字符串,就像+23是一个积极的23,她反对.

2 个回答
  • 该代码对我有用。(添加缺失的except子句/ import语句后)

    您是否\输入了原始代码?

    urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' \
                  + stock + '/chartdata;type=quote;range=5d/csv'
    

    如果您省略它,则可能是导致异常的原因:

    >>> stock = 'GOOG'
    >>> urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'
    >>> + stock + '/chartdata;type=quote;range=5d/csv'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: bad operand type for unary +: 'str'
    

    顺便说一句,string(e)应该是str(e)

    2023-02-10 13:21 回答
  • 你说这if int(splitLine[0]) > int(lastUnix):是造成麻烦的,但你实际上没有表明任何暗示的东西.我认为这一行是问题所在:

    print 'Pulled', + stock
    

    你明白为什么这一行会导致错误信息?你也想要

    >>> stock = "AAAA"
    >>> print 'Pulled', stock
    Pulled AAAA
    

    要么

    >>> print 'Pulled ' + stock
    Pulled AAAA
    

    >>> print 'Pulled', + stock
    PulledTraceback (most recent call last):
      File "<ipython-input-5-7c26bb268609>", line 1, in <module>
        print 'Pulled', + stock
    TypeError: bad operand type for unary +: 'str'
    

    你要求Python将+符号应用于一个字符串,就像+23是一个积极的23,她反对.

    2023-02-10 13:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有