Bottom Page

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
ichabod801 wrote Feb-04-2019, 03:51 AM:
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Quote
#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

Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 197 Oct-17-2019, 03:04 PM
Last Post: baquerik
  open, read and replace a string in a file Reims 0 133 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 261 Sep-15-2019, 01:24 PM
Last Post: buran
  Slicing Python list of strings into individual characters Drone4four 5 582 Apr-17-2019, 07:22 AM
Last Post: perfringo
  problems with python script and special characters last08 1 416 Mar-29-2019, 09:28 AM
Last Post: Kebap
  modify the color a string of characters in python 3.6 atlass218 10 844 Feb-28-2019, 03:20 PM
Last Post: atlass218
  problem in replace string into file Saeid_Bibak 1 425 Jan-20-2019, 09:46 PM
Last Post: Gribouillis
  Problem with Polish characters in xlsx file Mikser 4 702 Nov-18-2018, 06:22 PM
Last Post: Gribouillis
  replace html string with python variable ! evilcode1 4 737 Aug-30-2018, 03:14 PM
Last Post: snippsat
  Python regex with negative set of characters multiline sonicblind 2 605 Jul-30-2018, 08:43 PM
Last Post: sonicblind

Forum Jump:


Users browsing this thread: 1 Guest(s)