我試圖從JSON文件中提取一些值,但似乎沒有工作。另外,當我嘗試添加打印語句以查看它在哪裏被炸燬時,什麼也不打印。任何人都可以看到任何明顯的原因,爲什麼這不起作用?我唯一能想到的是,它與我最近從PC切換到Mac並且文件存儲爲rtf而不是txt的事實有關。因爲您創建和打開名爲f_n
在python腳本的目錄中的空文件,然後嘗試用json模塊加載它從JSON文件中檢索響應
import glob2
import json
fdr = glob2.glob('/Users/Lab-Justin/Desktop/CogStylesText/TextFiles/*.rtf') #insert all rtf files in folder into list
for dr in fdr: #loop through file list
print(dr)
ending = str(dr[57:]) #cut filename from pathname
pe = ending.replace('.rtf', '') #add filename to path
f_quest = '/Users/KraemerLab-Justin/Desktop/CogStylesText/CogStylesExcel/QuestEx/' + pe + '.csv' #format the file as csv
f_n = '/Users/Lab-Justin/Desktop/CogStylesText/TextFiles/' + pe #access rtf file
print(f_n)
file_path = 'f_n'
file_out = 'f_quest'
with open(file_path) as f_in, open(file_out) as f_out:
data = json.load(f_in)
print(data.keys()) # list the dicts keys
q = 'vviq'
response = data[q]['response']
f_out.write('response') #write responses to new .csv file
其中sql是在這個? –
我認爲你的意思是「JSON」而不是「SQL」?這些是非常不同的東西 – roganjosh
對,這些文件也存儲在SQL服務器上。對此感到抱歉。 – DartmouthDude82