Python Forum
Replace characters from file in Python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace characters from file in Python 2.7
#1
Hello

I can't replace more strings from file, i use two ways:
  • function lambda
  • expr regulier

#! /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()))
Output:
>cat fic.txt \xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah
Thanks for help
Reply
#2
What exactly are you trying to do? I'm not sure what result you are trying to get.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hello,
I use three methods to replace "\xe2\x80\x9c and" "\xe2\x80\x9d" :
The first give me a good result
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'))
Output:
http://www.google.com blah blah#%#@$^blah
but the others not ok
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()))
Output:
\xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah \xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah
I thank it's clear now.

Thanks for help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 2,850 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  How to do a mass replace within a CSV file? cubangt 9 5,198 May-09-2022, 06:52 PM
Last Post: snippsat
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,361 May-06-2022, 01:44 PM
Last Post: Larz60+
  Find and replace in files with regex and Python Melcu54 0 1,824 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,100 May-31-2021, 07:00 AM
Last Post: bowlofred
  Extract continuous numeric characters from a string in Python Robotguy 2 2,580 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Split Characters As Lines in File quest_ 3 2,470 Dec-28-2020, 09:31 AM
Last Post: quest_
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,726 Oct-19-2020, 04:16 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020