replace

Substitute a part of string (ignoring special characters)

Substitute a part of string (ignoring special characters) Question: Suppose that I have a list of strings, and I want to substitute different parts of it by re.sub. My problem is that sometimes this substitution contains special characters inside so this function can’t properly match the structure. One example: import re txt = ‘May enbd …

Total answers: 1

Is calling str.replace() twice the best solution for overlapping matches?

Is calling str.replace() twice the best solution for overlapping matches? Question: When I execute the following code I expect all ‘ a ‘ to be replaced by ‘ b ‘ yet only non overlapping matches are replaced. " a a a a a a a a ".replace(‘ a ‘, ‘ b ‘) >>>’ b a …

Total answers: 2

value error string can not be converted to float?

value error string can not be converted to float? Question: import scrapy from scrapy import Field,Item from itemloaders.processors import TakeFirst,MapCompose def get_price(txt): value = txt.replace(‘1.359,20′,’1.359’) value = txt.replace(‘,’,’.’) value = txt.strip(‘1.499,00’) value = txt.strip(‘R$’) float(value) class MultilaserItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() title = Field(output_process=TakeFirst()) price = …

Total answers: 2

replace a word in a column with values from another column in pandas dataframe

replace a word in a column with values from another column in pandas dataframe Question: In below table, I want to create a new column or update the Comment column that can replace the placeholder word "NameTag" with the value in column "Name" and the word "IDTag" with the value in column "ID" ID Name …

Total answers: 6

Replace substring with multiple words in python3

Replace substring with multiple words in python3 Question: I am trying to replace a word in a string with multilple words and produce all the strings as an output. For example ‘disease’ in ‘lysosome storage disease’ should be replaced by ‘disease’ , ‘diseases’, ‘disorder’, ‘disorders’,’syndrome’,’syndromes’ and produce following output. lysosome storage disease lysosome storage diseases …

Total answers: 2

Find line number of replaced key value

Find line number of replaced key value Question: How do I get the line number of replaced key value? currently functions are different for it, how do i combine it to have line number at the time of replacing the string. filedata= is a path of file. In which i need to replace strings. old_new_dict …

Total answers: 1

delete specific lines in txt file python

delete specific lines in txt file python Question: so I have text file file.txt e.g daemon nserver 1111 nserver 11111 nscache 65536 timeouts 1 5 30 60 180 1800 15 60 log /var/log/3proxy/log D logformat "- +_L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T" logformat "L%t.%. %N.%p %E %U %C:%c %R:%r %Q:%q %O …

Total answers: 1

How do I change 2 random variables out of 4?

How do I change 2 random variables out of 4? Question: I need to change the value of two random variables out of four to ‘—’. How do I do it with maximum effectiveness and readability? Code below is crap just for reference. from random import choice a = 10 b = 18 c = …

Total answers: 6