大家,早安,
我正在嘗試制作一個迷你軟體,我在其中讀取 csv,將其插入一個變數,然后將該變數提供給 check_call 函式。CSV 是一個資料庫串列:
cat test_db.csv
andreadb
billing
fabiodb
這就是我現在寫的:
from subprocess import *
import csv
#Load the CSV inside the variable data
with open('test_db.csv', 'r') as csvfile:
data = list(csv.reader(csvfile))
#For loop that per each database it shows me the tables and the output saved into risultato.txt
for line in data:
database = line
check_call[("beeline", "-e", "\"SHOW TABLES FROM \"", database, ";" , ">>" , "risultato.txt")]
當我執行它時,我收到以下錯誤:
Traceback (most recent call last):
File "test_query.py", line 10, in <module>
check_call[("beeline", "-e", "\"SHOW TABLES FROM \"", database, ";")]
TypeError: 'function' object has no attribute '__getitem__'
我對 python 比較陌生,這是我的第一個專案,所以任何幫助都會很棒。如果我沒有正確解釋某些內容,請告訴我,我會編輯帖子。謝謝!!
uj5u.com熱心網友回復:
您輸錯了函式呼叫。它應該是
check_call(["beeline", "-e", "\"SHOW TABLES FROM \"", database, ";" , ">>" , "risultato.txt"])
將(被放置后,[在你的問題。首先應該是(命令和引數串列。
uj5u.com熱心網友回復:
經過大量修補后,我找到了一種在此 check_call 中連接變數的方法:
for line in data:
i=0
database=str(line[i])
check_call(["beeline -e \"SHOW TABLES FROM " database "\" >> risultato.txt"])
i =i
執行后,將其保存在 risultato.txt 后會產生正確的輸出:
-----------
| tab_name |
-----------
| result |
| results |
-----------
------------------------------------
| tab_name |
------------------------------------
| tab_example_1 |
| tab_example_2 |
------------------------------------
---------------------------------------
| tab_name |
---------------------------------------
| tab_example_3 |
| tab_example_4 |
---------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/386968.html
