在處理字典文件或大量文本數據時,去重是常見需求。以下展示兩種 Python 實作方式,均能保持原有順序不打亂:
#coding=utf-8
import sys
def open_txt():
try:
with open(sys.argv[1], 'r') as f:
for line in f.readlines():
passlist.append(line)
except:
return 0
def write_txt():
try:
with open(sys.argv[2], 'w') as f:
for i in list_passwed:
f.write(i)
except:
return 0
passlist = []
open_txt()
# 列表去重,保持原順序
list_passwed = []
for i in passlist:
if i not in list_passwed:
list_passwed.append(i)
write_txt()
def open_txt():
try:
with open('admin.txt', 'r') as f:
for line in f.readlines():
passlist.append(line)
except:
return 0
passlist = []
open_txt()
list_passwed = []
for i in passlist:
if i not in list_passwed:
list_passwed.append(i)
當處理複雜表格時,常遇到列寬無法按預期控制的問題。table-layout:fixed 是解決此類問題的關鍵屬性。
當表格包含長英文字串(無空格)需強制換行且不撐破表格時,傳統調整第一行 width 無效,此時 table-layout:fixed 能完美解決。
這三個標籤用於對表格行進行分組,提升可訪問性和打印體驗:
| 標籤 | 功能 | 打印行為 |
|---|---|---|
| thead | 表格頭部,可單獨定義樣式 | 分頁時頂部重複打印 |
| tfoot | 表格頁腳,可單獨定義樣式 | 分頁時底部重複打印 |
| tbody | 表格主體,分段顯示長數據 | 正常顯示 |
<table>
<thead><tr><th>Month</th><th>Date</th></tr></thead>
<tfoot><tr><th>Month Lists</th><th>Date Lists</th></tr></tfoot>
<tbody><tr><td>AUG</td><td>18</td></tr></tbody>
</table>
注意:必須按 thead → tfoot → tbody 順序寫入,且 thead 內必須包含 tr 標籤。
這兩個標籤用於定義列屬性,但兼容性較差。實測僅 width 和 background 兩屬性較可靠。
col:定義單列或多列屬性colgroup:列分組以便格式化建議:能不用盡量不用,第一行設定寬度更穩妥。
| 屬性 | 值 | 作用 |
|---|---|---|
| word-wrap | normal | break-word | 控制單詞是否斷行 |
| word-break | normal | break-all | keep-all | 控制字符級斷行 |
| white-space | normal | pre | nowrap | 控制空白符處理 |
table { table-layout: fixed; }
td, th {
word-wrap: break-word;
word-break: break-all;
}
/* 若仍有問題,內層加 div */
td > div {
word-wrap: break-word;
word-break: break-all;
}
td > div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* 可選:溢出顯示省略號 */
}
/* IE td 有寬度且無標點時需加 */
word-break: keep-all;
word-wrap:break-word:標準塊級元素有效,table 元素需配合 table-layout:fixedword-break:break-all:Chrome 支持 table 元素,Firefox/Opera 不支持white-space:nowrap:IE td 有寬度且無標點失效,需包裹 div 解決綜上,無完美單屬性方案,需組合使用並輔以結構調整。極致兼容仍需 JavaScript 兜底。