2017-02-23 9 views
0

私はPythonで新しく、Oracleテーブルのデータを配列に追加し、別の値をレコードのdatetime(文字列)として追加しようとしています。タプルdatetimeに参加するには

私のコードは次のとおりです。使用しようとした

('1110', '1000074', 2060,'2017-02-23 11:57:41') 

('1110', '1000074', 2060, '2', '0', '1', '7', '-', '0', '2', '-', '2', '3', ' ', '1', '1', ':', '5', '2', ':', '0', '2') 

は、私は、出力は次のようになりますので、日付に参加したい:

now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
con = cx_Oracle.connect('user', 'pass', dsn_tns) 
q='SELECT columns FROM table' 

device_status = [] 
cursor = con.cursor() 
cursor.execute(q) 
results = cursor.fetchall() 

for row in results: 
    device_status.append(row + tuple(now)) 

con.close() 
print device_status[1] 

これが出力されます参加しましたが、次のエラーが発生しました:

can only concatenate tuple (not "str") to tuple 

私は間違っていますか?

答えて

1

小さな変化、

for row in Results: 
    device_status.append(row + (now,)) 
           ^

,タプルとしてこのメ​​イク。したがって、両方ともタプルになります。

そしてtuple(now)は、

In [42]: a = '2017-02-23 11:57:41' 
In [43]: print tuple(a) 
('2', '0', '1', '7', '-', '0', '2', '-', '2', '3', ' ', '1', '1', ':', '5', '7', ':', '4', '1') 

が作業を参照してください、このよう

In [34]: time_ 
Out[34]: '2017-02-23 15:33:42.353505' 
In [35]: (1,2,45.7,799)+(time_,) 
Out[35]: (1, 2, 45.7, 799, '2017-02-23 15:33:42.353505') 


In [36]: (1,2,45.7,799)+(time_) # tried with out , and gets an error 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-36-551a4c2dc8d7> in <module>() 
----> 1 (1,2,45.7,799)+(time_) 

TypeError: can only concatenate tuple (not "str") to tuple 
+0

感謝のすべての値を分割します。私にはうまくいく。 –

1

編集:タプル要素にインデックスでアクセスできることを学びました。あなたはその後、device_statusリストにこの値を追加することができ

>>> a 
('1110', '1000074', 2060, '2', '0', '1', '7', '-', '0', '2', '-', '2', '3', ' ', '1', '1', ':', '5', '2', ':', '0', '2') 
>>> print(a[0], a[1], a[2], (''.join(a[3:]))) 
('1110', '1000074', 2060, '2017-02-23 11:52:02') 

:あなたはこのような何かを行うことができます。

これは、探しているタイプの最初の3つの値と、それ以降のインデックス3から日時の値に依存します。お役に立てれば。

関連する問題