Error message in python

563
9
12-17-2013 07:03 PM
minghuichua
New Contributor
Hi,

Anyone here can help me solve this error that I encounter during my execution?

File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__

  File "C:\Users\Administrator\Desktop\Minghui\Phyton\Testing.py", line 20, in <module>
    raw_new = compulsory + raw
TypeError: cannot concatenate 'str' and 'float' objects

Greatly appreciated.

Regards,
Minghui
Tags (2)
0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus
From the error message you appear to be trying to concatenate a string and a number.  I suspect "raw" is a number, hence it needs to be converted to a string using str(raw) prior to combining them
0 Kudos
minghuichua
New Contributor
Thanks for the help. But I got another error asking me to define my Query_value1 when I've already define it. Below are the error message and part of my code.

Error message:

File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Administrator\Desktop\Minghui\Phyton\Testing.py", line 25, in <module>
    for tags in Query_value1:
NameError: name 'Query_value1' is not defined

My code are below:


Query_value1 = fetcher.execute("select value1 from [Sheet2$]").fetchall()

tagname = []   #Tag name list with Bucket Brigade. prefix
for tags in Query_tag:
    raw = tags[-1]
    compulsory = 'Bucket Brigade.'
    raw_new = compulsory + str(raw)
    tagname.append(raw_new)


value1 = []  #1st column of data
for tags in Query_value1:
    raw = tags[-1]
    value1.append(raw)

Regards,
Minghui
0 Kudos
DanPatterson_Retired
MVP Emeritus
That is a new error...did this script ever work in stages? You might want to post what you are trying to do and a complete list of the error messages...I suspect your code isn't using "try...except" blocks so you can trap errors and their nature during execution
0 Kudos
minghuichua
New Contributor
Currently I'm trying to send real-time data from excel spreadsheet to MatrikonOPC. I'm testing out with random data in excel sheet with my value 1 in the column B of the spreadsheet.

Below are my code:

import OpenOPCEdited
import pyodbc
import time

opc = OpenOPCEdited.client()
opc.connect('Matrikon.OPC.Simulation.1')

cnxn = pyodbc.connect('DSN=Excel Files;Dbq=C:\Users\Administrator\Desktop\Minghui\Testing.xlsm', autocommit=True)
fetcher = cnxn.cursor()

Query_tag = fetcher.execute("select tag from [Sheet2$]").fetchall()

Query_value1 = fetcher.execute("select value1 from [Sheet2$]").fetchall()


tagname = []   #Tag name list with Bucket Brigade. prefix
for tags in Query_tag:
    raw = tags[-1]
    compulsory = 'Bucket Brigade.'
    raw_new = compulsory + str(raw)
    tagname.append(raw_new)


value1 = []  #1st column of data
for tags in Query_value1:
    raw = tags[-1]
    value1.append(raw)

interval = 60               # update interval in seconds

#Writing data to OPC
for iteration in range(2):
    n = 0
    m = n + 1
    for every in tagname:
        find_cb = every.find('CB')
        if find_cb == -1:
            opc[every] = value1
        if find_cb != -1:
            opc[every] = str(value1)
        n += 1
   
    time.sleep(interval)   #wait (X) seconds
   
Sorry for the trouble as I'm really a newbie in this stuff.
Greatly appreciated.

Regards,
Minghui
0 Kudos
DanPatterson_Retired
MVP Emeritus
Insert your code in code blocks, it is largely unreadable since it lacks proper indentation etc
0 Kudos
minghuichua
New Contributor
erm.. you mean insert in code block here? I've attach my code here too
0 Kudos
DanPatterson_Retired
MVP Emeritus
No...code blocks is an html formatting code which you use to highlight your code on this forum.   Posting a *.py script just causes it to run (and fail) and people are less inclined to download...save...and debug code that could be viewed if properly documented and formatted on this site.  On an aside...this code is quite long...at what line did you first start getting error messages and are they all documented?
0 Kudos
minghuichua
New Contributor
Cause this code was given to me by my professor in school.
Was able to execute previously by him. So I have to make amendment to it according to my excel spread sheet. But after making some amendment I'm not sure why the Query_value1 was not define when I did not change anything there. So I'm quite stuck at this stage
0 Kudos
minghuichua
New Contributor
Hey thanks for the help. Somehow I manage to solve my problem and my data can be read into MatrikonOPC Explorer.

Do you know like what I need to include in order for python to auto update value in MatrikonOPC?
0 Kudos