Replace characters from file in Python 2.7
Bonjour,
Je na'arrive pas à remplacer plusieurs strings ds un fichier en utilisant:
- function lambda
ou - re.sub avec expr régulières
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#! /bin/python
import re
import sys
import fileinput
import os
stripped = lambda s: "".join(i for i in s if 31 < ord(i) < 127)
#en dur ça marche
print(stripped('\xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah'))
for line in fileinput.input('fic.txt'):
#with exrp regu
line1 = re.sub(r'[^\x00-\x7f]', r'', line.rstrip())
print(line1)
####################
#with lambda
print(stripped(line.rstrip())) |
Merci de votre aide
Replace characters from file in Python 2.7
Le contenu est:
Code:
1 2
|
\xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah |
Replace characters from file in Python 2.7
C'est vrai ça marche quand on passe la chaîne de caractère en dur.
Mais ça ne fonctionne pas quand cette chaîne de caractère est dans un fichier 'fic.txt', il fait pas le replace.