LoginSignup
23
15

More than 5 years have passed since last update.

ファイル名として無効な文字を削除(Python)

Posted at

Webスクレイピングでデータ取得していたときに、エラーが発生したのでメモ
Windowsではファイル名として無効文字\/:*?"<>|があるのでそれを削除する

re.sub(pattern, repl, string)

文字列置換にはreplace()があるが、正規表現で処理できないのでre.sub()を用いる
使い方は、re.sub('置換対象文字','置換文字','入力文字列')

re.sub
import re
string='\gbahsd:njs?<>|"asd/as*'

print(re.sub(r'[\\/:*?"<>|]+','',string)
output
'gbahsdnjsasdas'

以上

23
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
15