2017-08-03 31 views
0

我想在一個文本文件中的所有字符的列表,除外獲取存在於一個文本文件(有例外)都是唯一的字符(Python)的

[A-Z], [0-9], '|', '~'. 

感謝你的幫助的列表。

+0

您是否嘗試過在你自己的東西嗎?如果是,請在此發佈。 –

+0

編輯制定規範標題。即使這個問題表明缺乏努力,它應該對未來的讀者有用。 –

+0

[This](https://stackoverflow.com/questions/2991901/regular-expression-any-character-that-is-not-a-letter-or-number)stackoverflow問題可能有幫助.. –

回答

1

步驟1:讀入您的文件並將其轉換爲一組字符。

charset = set(open('file.txt').read()) 

步驟2:與str.join用於下一步驟加入回一個字符串。

chars = ''.join(charset) 

步驟3:使用正則表達式,替換你不''希望所有的字符,然後顯示

import re 
filtered_chars = re.sub('[A-Z0-9|~]', '', chars) 

print(set(filtered_chars)) 

其他參考資料(類似於你的使用情況,但不完全) :

  1. List of all unique characters in a string?

  2. How to get all unique characters in a textfile? unix/python

  3. Regular Expression: Any character that is NOT a letter or number

+0

非常感謝你。 – rg1105

相關問題