我有幾個檔案要加載我想將它們分為內部有“內容...”行的檔案和沒有內容的檔案,但我的程式將所有內容都放入名稱為空的字典中。
from tkinter import Tk
from tkinter.filedialog import askopenfilenames
Tk().withdraw()
filename = askopenfilenames() # show an "Open" dialog box and return the path to the selected file
print(filename)
content = {}
for file in filename:
fp = open(file, 'r')
lines = fp.readlines()
x ='Content'
content = {'is_in':[],'empty':[]}
with open(file) as myfile:
head = [next(myfile) for x in range(0,4)]
for line in head:
if x in line:
whole_line = line
whole_line = ''.join(whole_line.split())
result = whole_line.replace('Content','')
content['is_in'].append(file)
elif result not in head:
content['empty'].append(file)
uj5u.com熱心網友回復:
這里的問題是如何使用scopeforresult變數。
如果在第一次迭代時,它會得到if x in line回報False,elif result not in head但此時result尚未定義。
此外,如果第一次迭代if x in line回傳True,它甚至不會檢查elfi陳述句。將繼續進行下一次迭代,假設下一次將達到elfi。在這里你仍然沒有result定義,因為它result是在if陳述句內的前一個回圈中定義的。
請閱讀有關python范圍的資訊
uj5u.com熱心網友回復:
至于我,你的一切elif毫無意義。
您應該首先檢查所有行head并設定 ie。found = True并在for回圈后使用此變數添加到content['empty']
searched = 'contente' # I will compare with `line.lower()`
# --- before `for`-loop ---
found = False
# --- `for`-loop ---
for line in head:
if searched in line.lower(): # compare lower case
content['is_in'].append(file)
found = True
break # there is no need to search in next lines
# --- after `for`-loop ---
if not found:
content['empty'].append(file)
最終你可以用content['is_in']這個
searched = 'contente' # I will compare with `line.lower()`
# --- `for`-loop ---
for line in head:
if searched in line.lower(): # compare lower case
content['is_in'].append(file)
break # there is no need to search in next lines
# --- after `for`-loop ---
if file not in content['is_in']:
content['empty'].append(file)
Python 也有特殊的結構for/else/break,它else在break不使用時 運行for
searched = 'contente' # I will compare with `line.lower()`
# --- `for`-loop ---
for line in head:
if searched in line.lower(): # compare lower case
content['is_in'].append(file)
break # there is no need to search in next lines
else: # in the same column as `for` (not as `if`)
content['empty'].append(file)
# --- after `for`-loop ---
具有其他更改的完整作業示例。
我在評論中保留了一些想法。
from tkinter import Tk
from tkinter.filedialog import askopenfilenames
root = Tk()
root.withdraw()
all_filenames = askopenfilenames() # show an "Open" dialog box and return the path to the selected file
print('all_filenames:', all_filenames)
root.destroy() # remove it from memory
content = {
'is_in': [],
'empty': [],
}
searched = 'contente' # I will compare with `line.lower()`
for filename in all_filenames:
with open(filename) as file:
head = [next(file) for _ in range(4)]
# --- befere `for`-loop ---
found = False
# --- `for`-loop ---
for line in head:
#if line.lower().startswith(searched): # compare lower case
if searched in line.lower(): # compare lower case
content['is_in'].append(filename)
found = True
break # there is no need to search in next lines
#else:
# content['empty'].append(filename)
# --- after `for`-loop ---
#if not filename in content['is_in']:
if not found:
content['empty'].append(filename)
# ---
print('is_in:', content['is_in'])
print('empty:', content['empty'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/356596.html
上一篇:如何計算串列框中具有相同文本的專案數量(Tkinter)
下一篇:網路安全專欄——telnet遠程登錄資料包捕獲個性化登錄賬號及密碼(圖文豐富 保姆級 有幾種錯誤解決方案 為什么不用輸入telnet密碼就能登錄 )
