replace « string « Python Data Type Q&A

Home
Python Data Type Q&A
1.array
2.date
3.dictionary
4.Format
5.integer
6.List
7.numpy
8.regex
9.string
10.tuple
Python Data Type Q&A » string » replace 

1. Python distutils and replacing strings in code    stackoverflow.com

I often find a need to put paths in my code in order to find data or in some cases tool-specific modules. I've so far always used autotools because of ...

2. How to work with unicode in Python    stackoverflow.com

I am trying to clean all of the HTML out of a string so the final output is a text file. I have some some research on the various 'converters' ...

3. What is wrong with my attempt to do a string replace operation in Python?    stackoverflow.com

What am I doing wrong here?

import re
x = "The sky is red"
r = re.compile ("red")
y = r.sub(x, "blue")
print x  # Prints "The sky is red"
print y  # Prints "blue"
How ...

4. Python Case Insensitive Replace    stackoverflow.com

What's the easiest way to do a case-insensitive str.replace in Python?

5. What's a good way to replace international characters with their base Latin counterparts using Python?    stackoverflow.com

Say I have the string "blöt träbåt" which has a few a and o with umlaut and ring above. I want it to become "blot trabat" as simply as possibly. I've ...

6. Optimal way to replace characters in large string with Python?    stackoverflow.com

I'm dealing with cleaning relatively large (30ish lines) blocks of text. Here's an excerpt:

PID|1||06225401^^^PA0^MR||PATIENT^FAKE R|||F PV1|1|I|||||025631^DoctorZ^^^^^^^PA0^^^^DRH|DRH||||... ORC|RE||CYT-09-06645^AP||||||200912110333|INTERFACE07 ...

7. String replacing in a file by given position    stackoverflow.com

I have a file opened in 'ab+' mode. What I need to do is replacing some bytes in the file with another string's bytes such that: FILE:

thisissomethingasperfectlygood.
string:
01234
So, for example, I seek for the ...

8. How to do a string replace in a urlencoded string    stackoverflow.com

I have a string like x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&format=json" If I do x % 10 that fails as there are %20f etc which are being treated as format strings, so I have to ...

9. Replacing one character of a string in python    stackoverflow.com

In python, are strings mutable? The line someString[3] = "a" throws the error

TypeError: 'str' object does not support item assignment
I can see why (as I could ...

10. Replace newlines in a Unicode string    stackoverflow.com

I am trying to replace newline characters in a unicode string and seem to be missing some magic codes. My particular example is that I am working on AppEngine and trying to ...

11. Replace a string of characters with some special characters?    stackoverflow.com

when i input some texts/strings, i want them to appear either as hidden or with some special characters(as we do while entering password). . . how can i implement it in ...

12. replace multiple string in a file    stackoverflow.com

I am trying to replace multiple string in a file.

fp1 = open(final,"w")
data = open(initial).read()
for key, value in mydict.items():
    fp1.write(re.sub(key,value, data)
fp1.close()
But only my last key value get replaced. how ...

13. How to replace empty string with zero in comma-separated string?    stackoverflow.com

"8,5,,1,4,7,,,,7,,1,9,3,6,,,8,6,3,9,,2,5,4,,,,,3,2,,,7,4,1,1,,4,,6,9,,5,,,,5,,,1,,6,3,,,6,5,,,,7,4,,1,7,6,,,,8,,5,,,7,1,,3,9," I'm doing a programming challenge where i need to parse this sequence into my sudoku script. Need to get the above sequence into 8,5,0,1,4,7,0,0,0,7,0,1,9,3,6,0,0,8......... I tried re but without success, help is appreciated, ...

14. How can I do the multiple replace in python?    stackoverflow.com

As asked and answered in HERE, I need to replace '[' with '[[]', and ']' with '[]]'. I tried to use s.replace(), but as it's not in place change, I ran ...

15. I'm writing a spellchecking program, how do I replace ch in a string?    stackoverflow.com

What am I doing wrong/what can I do?

import sys
import string

def remove(file):
    punctuation = string.punctuation
    for ch in file:
        ...

16. Python adding elements from string    stackoverflow.com

I have a string like this "1 1 3 2 1 1 1 ...

17. Replacement for PyString_AS_STRING in python 3.x    stackoverflow.com

In python 2.x versions there is a function named as PyString_AS_STRING to convert a pyobject pointer to a string or char pointer. How can we achieve the same functionality in python 3? ...

18. case sensitive string replacement in Python    stackoverflow.com

I need to replace a string in case sensitive way. For example

abc -> def
Abc -> Def
aBc -> dEf
abC -> deF
What can I do this with Python?

19. Multiple character replace with python    stackoverflow.com

I need to replace some characters as follows : & -> \&, # -> \#, ... I coded as follows, but I guess there should be some better way. Any hints?

strs = ...

20. Python string.replace() not replacing characters    stackoverflow.com

Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the "normal" extensions (.doc, .xls, .ppt). They are all ...

21. Issue Replacing Already Existing Strings with ConfigParser    stackoverflow.com

I am using ConfigParser to save simple settings to a .ini file, and one of these settings is a directory. Whenever I replace a directory string such as D:/Documents/Data, with a ...

22. How to replace the some characters from the end of a string?    stackoverflow.com

My question is very simple, but I didn't get the answer from google. There is a python string:

 s = "123123"
I want to replace the last 2 with x. Suppose there is ...

23. How to convert unicode string like u'\\u4f60\\u4f60' to u'\u4f60\u4f60' in Python?    stackoverflow.com

I capture the string from a html source file using regex:

f = open(rrfile, 'r')
p = re.compile(r'"name":"([^"]+)","head":"([^"]+)"')
match = re.findall(p, f.read())
And I've tried:
>>> u'\\u4f60\\u4f60'.replace('\\u', '\u')  
u'\\u4f60\\u4f60'  
>>> u'\\u4f60\\u4f60'.replace(u'\\u', '\u') ...

24. Replacing some part of string with Python    stackoverflow.com

I have a SQL string, for example

SELECT * FROM benchmark WHERE xversion = 1.0
And actually, xversion is aliased variable, and self.alias has all the alias info something like
{'CompilationParameters_Family': 'chip_name', 
 ...

25. Named replaces in strings with Mako    stackoverflow.com

When creating a template in Mako, I would need to write things like : ${_('Hello, %(fname)s %(lname)s') % {'fname':'John','lname':'Doe'}} I keep getting SyntaxException: (SyntaxError) unexpected EOF while parsing when writing that. Is ...

26. How to replace repeated instances of a character with a single instance of that character in python    stackoverflow.com

I want to replace repeated instances of the "*" character within a string with a single instance of "*". For example if the string is "***abc**de*fg******h", I want it to get ...

27. Replace string within file contents    stackoverflow.com

How can I open a file, Stud.txt, and then replace any occurences of "A" with "Orange"?

28. Forward slash in Python replace()    stackoverflow.com

I have been working on an anagram solver in Python 2.7 and came across a curiosity that I haven't been able to find an explanation for. The program reads from a ...

29. Replace \n with
    stackoverflow.com

I'm parsing text from file with Python. I have to replace all newlines (\n) with
cause this text will build html-content. For example, here is some line from file:

'title\n'
Now ...

30. Python replace not working with list    stackoverflow.com

My code reads a directory and stores the filename with extention into a list. What I am trying to do is get rid of the extention with replace. However, ...

31. Replace special characters in python    stackoverflow.com

I have some text coming from the web as such: £6.49 Obviously I would like this to be displayed as: £6.49 I have tried the following so far:

s = url['title']
s = s.encode('utf8')
s = s.replace(u'Â','')
And a ...

32. python: Replacing special characters in a string    stackoverflow.com

I read the artist of a song from its MP3 tag, then create a folder based on that name. The problem I have is when the name contains a special ...

33. Python Replace \\ with \    stackoverflow.com

So I can't seem to figure this out... I have a string say, "a\\nb" and I want this to become "a\nb". I've tried all the following and none seem to work;

>>> ...

34. How to replace string values inside plpython?    stackoverflow.com

Hi i need to replace value into null value in plpython, but how can i do that?Can somebody help? for example inside plpython sql i wrote.

aa = 'World 123'
bb = aa.replace('World ', ...

35. Problem with Replacing special characters in a string    stackoverflow.com

I am trying to feed some text to a special pupose parser. The problem with this parser is that it is sensitive to ()[] characters and in my sentence in ...

36. Python string replace in a file without touching the file if no substitution was made    stackoverflow.com

What does Python's string.replace return if no string substitution was made? Does Python's file.open(f, 'w') always touch the file even if no changes were made? Using Python, I'm trying to replace occurrences of ...

37. Refer to same input multiple times in string replacement?    stackoverflow.com

I need to print something like this

"a=name1,b=name2,c=name1,d=name2"
While I have name1 and name2 in variable n1 n2
n1="name1"
n2="name2"
what I am trying to do
"a=%s,b=%s,c=%s,d=%s" % (n1,n2,n1,n2)
Is there a better way than this? way ...

38. Replace first occurrence only of a string?    stackoverflow.com

I have something like this:

text = 'This text is very very long.'
replace_words = ['very','word']

for word in replace_words:
    text = text.replace('very','not very')
I would like to only replace the first ...

39. Python: Replace ith occurence of x with ith element in list    stackoverflow.com


Suppose we have a string a = "01000111000011" with n=5 "1"s. The ith "1", I would like to replace with the ith character in "ORANGE". My result should look like:

b = "0O000RAN0000GE"
What ...

40. How do I replace part of a large string in Python?    stackoverflow.com

I have a file with multiple lines, each having a long sequence of characters (no spaces). For example in one line: qwerrqweqweasdqweqwe*replacethistext*asdasdasd qwerrqweqweasdqweqwe*withthistext*asdasdasd The specific string I am looking for can happen any where ...

41. String replace utility conversion from Python to F#    stackoverflow.com

I have a simple python utility code that modifies the string line by line. The code is as follows.

import re

res = ""
with open("tclscript.do","r") as f:
    lines = ...

42. string suffix replacement in python    stackoverflow.com

i know how to make string replacements in python but I need a way to replace only if the sequence is located at the end of the word. for example:

rule: at -> ate
so:
cat ...

43. string replace vowels in python?    stackoverflow.com

Expected:

>>> removeVowels('apple')
"ppl"
>>> removeVowels('Apple')
"ppl"
>>> removeVowels('Banana')
'Bnn'
CODE (Beginner):
def removeVowels(word):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for c in word:
        if ...

44. Replacing a weird single-quote (’) with blank string in Python    stackoverflow.com

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka \xe2 aka #8217). But when I run that line of code, I get this error:

SyntaxError: Non-ASCII character ...

45. Replace string does not work in python 3.2    stackoverflow.com

I have a template file: 'template.txt' like below:

class Core_Model_DbTable_{table_name} extends YouNet_Db_Table
{
    const TYPE_PRINTED      = 1;
    const ...

46. What is the recommended way to replace multiple strings in one large string in Python?    stackoverflow.com

I have many string pairs and one large string (which is the content of a file). I need to replace each and every occurrence of the first member in each pair ...

47. %s Not Replacing Information    stackoverflow.com

I am trying to put information available on the web, and to do so I am using string formatting:

fields = """{"fname" : %s, "lname" : %s, "email" : %s, "deptid" : ...

48. Replacing parts of the string    stackoverflow.com

Actually my question is quite simple: I have a string like: 101222_1_1_ab; I hope to change it into: 101222_1_ab_1 What bash commands should I use? and what to do in python? THanks

49. Replace String by python    stackoverflow.com

i have list

http://lincoln.com/picture/2453345/flower.jpg
http://lincoln.com/picture/2354345/flower1.jpg
replace into:
http://lincoln.com/picture/4453345/flower.jpg
http://lincoln.com/picture/4354345/flower1.jpg
i tried:
f=open('fileinput','r')
f.replace('2453345/flower.jpg','4453345/flower.jpg')
but i have more line.i do it spend a lot time ...

51. replacing characters within a string    bytes.com

Hi, I coded a python script that lets me parse a csv file into html code with a certain format, but I would like to replace every "<" and ">" character that appears within each column entry of the csv file (they are parsed as strings) with the html equivalents of "<" and ">". example csv file row: "FIXED","All","Enable

52. Error Occurs: Replace a character in a String    dreamincode.net

Hello, I am getting an error in my python script when I try to change a character in a string. But I dont know why or what to do to fix it? I have commented in my code where the error occurs def format_file(filename): HTML_file = open(filename,'r') HTML_source = HTML_file.read() HTML_file.close() x = 0 # Format all ID's & classes correctly ...

54. String Replace Issue with Road RD to Rd to rd    python-forum.org

The ulimtate purpose of the script will be to replace 1St with 1st, 2Nd with 2nd, ect in my address field of my .shp file. I want the script to find and replace a string in my adress field IF the adress City field is null. The script runs with no errors, but when I go look at the table, nothing ...

55. how to replace single value in string    python-forum.org

56. Replacing element in a string    python-forum.org

57. Replacing occurence of a string according to a list    python-forum.org

So I have a text of the kind: "See *** for more information about the topic. Refer to *** in orde to..." I then have a list of strings which I wish to replace each *** with. So that the first *** is replaced by list[0], the second *** by list[1] and so forth. The pattern is always *** I've tried ...

58. String.Replace does not work with unicode?    python-forum.org

59. Very odd string replacement behaviour    python-forum.org

Code: Select all class TEST: def __init__(self): self.dotV = [u'_z1', u'_z2'] self.gzV = [u'_z2', u'-2*0.5*1*_z2-1**2*(_z1+0.1*_z1**3)'] def g_ki(self): """ ...

60. String Replace Problem    python-forum.org

That's just for demonstration purposes. A regular string containing backslashes often shows as a string with double backslashes, which can be confusing. So in order to show the "real" string used, I used the raw format. IOW, when a string comes in from a file, you can use it "as is", no conversion needed. Actually, it is already a "raw string" ...

61. Replace character in string    python-forum.org

Why are you using a string to represent the board? If you use a list, and then you can do index replacements easily. You could even have a list of lists for more intuitive indexing. Generally when I'm making games in Python, I use a list or dictionary to represent the board, and then generate strings for user output as needed. ...

62. string.replace    python-forum.org

You're welcome. That's not an error I've seen before, but I can think of a couple of things that might be causing it. Could you post the input text that causes errors, or make a reduced test-case that gives the error with just a small (postable amount) part of the input text? It's probably an encoding issue, but more information is ...

63. string replacement    python-forum.org

64. String replacing beginner question    python-forum.org

65. string replacement    python-forum.org

Hello everyone! Im new to python however Ive written in loads of languages, so not very hot on python at the mo! What Im trying to do is read in a simple text file (version.as) and then process some string replacement and re-write the file. There will be a few different variables to change for example @BUILDDATE@ I have no problems ...

66. string replacement within a file    python-forum.org

def parseDataFile(fn, beginMk, endMk): f = open(fn) # skip to 'essential' section s = f.next() while s.strip() != beginMk: s = f.next() outList = [] for line in f: ...

67. Replace strings in a file from another file    python-forum.org

Hello, I'm a python beginner and I have the following problem: I need to replace SOME strings in a file, with strings in another file. It is a translation of a story from a language to the other. In practice I have: FILE1 in english, with some phrases to be replaced (not consecutive, can be even in middle of other things) ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.