Skriptanfänger schrieb:
>
1 | > Traceback (most recent call last):
|
2 | > File "./mysql.py", line 15, in <module>
|
3 | > print row["ID"] + "+" + row["solarVoltage"]
|
4 | > TypeError: unsupported operand type(s) for +: 'long' and 'str'
|
5 | >
|
Tja: das eine Datenfeld enthält ein Integer, das andere einen String,
die kannst Du nicht mit + konkatenieren. Du kannst stattdessen
1 | print '%d+%s'%(row['ID'], row['solarVoltage'])
|
oder
1 | print str(row['ID']) + '+' + row['solarVoltage']
|
benutzen.
> und danach möchte ich die ausgabe im print automatisch in json format
>
> nach dem Format
>
>
1 | > {
|
2 | > "1": {
|
3 | > "solarVoltage": "xxxV"
|
4 | > },
|
5 | > "2": {
|
6 | > "solarVoltage": "xxxV"
|
7 | > },
|
8 | > "3": {
|
9 | > "solarVoltage": "xxxV"
|
10 | > }
|
11 | > }
|
12 | >
|
Das kannst Du einfach umwandeln:
1 | dumps( { x['ID']: {'solarVoltage': x['solarVoltage'] } for x in rows})
|
IMHO eleganteres JSON kommt aber vermutlich heraus, wenn Du es so
machst: