Replace Some Special Character With Newline \n
Solution 1:
The re.findall
is useless here.
withopen('path/to/file') as infile:
contents = infile.read()
contents = contents.replace('-3","', '\n')
print(contents)
Another problem with your code is that you seem to think that "-3",""
is a string containing -3","
. This is not the case. Python sees a second "
and interprets that as the end of the string. You have a comma right afterward, which makes python consider the second bit as the second parameter to s.replace()
.
What you really want to do is to tell python that those double quotes are part of the string. You can do this by manually escaping them as follows:
some_string_with_double_quotes = "this is a \"double quote\" within a string"
You can also accomplish the same thing by defining the string with single quotes:
some_string_with_double_quotes = 'this is a "double quote" within a string'
Both types of quotes are equivalent in python and can be used to define strings. This may be weird to you if you come from a language like C++, where single quotes are used for characters, and double quotes are used for strings.
Solution 2:
First I think that the s object is not a string but a list and if you try to make is a string (s=''.join(s) for example) you are going to end with something like this:
0000443047431431000044304743143177980351931000030049294773100006224433874315127544983100003323786067
Where replace() is useless.
I would change your code to the following (tested in python 3.2)
lines = [line.strip() for line inopen('text.txt')]
line=''.join(lines)
cl=line.replace("-3\",\"","\n")
print(cl)
Post a Comment for "Replace Some Special Character With Newline \n"