how to reformat a text paragrath using python [closed]
Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.
python string text
closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.
python string text
closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something likesed ‘s/,/,n/’
– cole
Nov 13 '18 at 20:42
Possible duplicate of Split a string by a delimiter in python
– mrk
Nov 13 '18 at 21:13
add a comment |
Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.
python string text
Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.
python string text
python string text
asked Nov 13 '18 at 20:39
bmanbman
1
1
closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by darthbith, Matthieu Brucher, Unheilig, Hoopje, shim Nov 13 '18 at 22:49
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something likesed ‘s/,/,n/’
– cole
Nov 13 '18 at 20:42
Possible duplicate of Split a string by a delimiter in python
– mrk
Nov 13 '18 at 21:13
add a comment |
Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something likesed ‘s/,/,n/’
– cole
Nov 13 '18 at 20:42
Possible duplicate of Split a string by a delimiter in python
– mrk
Nov 13 '18 at 21:13
Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like
sed ‘s/,/,n/’
– cole
Nov 13 '18 at 20:42
Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like
sed ‘s/,/,n/’
– cole
Nov 13 '18 at 20:42
Possible duplicate of Split a string by a delimiter in python
– mrk
Nov 13 '18 at 21:13
Possible duplicate of Split a string by a delimiter in python
– mrk
Nov 13 '18 at 21:13
add a comment |
2 Answers
2
active
oldest
votes
You can do using str.replace() in python. Check out the below code, replacing every ,
with ,n
.
string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)
File before execution:
Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,
After Execution:
Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,
add a comment |
you can use the ''.replace()
method like so:
'roses can be blue, red, white'.replace(',' , ',n')
gives 'roses can be blue,n red,n white'
efectively inserting 'n'
after every ,
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do using str.replace() in python. Check out the below code, replacing every ,
with ,n
.
string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)
File before execution:
Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,
After Execution:
Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,
add a comment |
You can do using str.replace() in python. Check out the below code, replacing every ,
with ,n
.
string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)
File before execution:
Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,
After Execution:
Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,
add a comment |
You can do using str.replace() in python. Check out the below code, replacing every ,
with ,n
.
string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)
File before execution:
Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,
After Execution:
Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,
You can do using str.replace() in python. Check out the below code, replacing every ,
with ,n
.
string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)
File before execution:
Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,
After Execution:
Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,
answered Nov 13 '18 at 20:51
Sanchit KumarSanchit Kumar
36319
36319
add a comment |
add a comment |
you can use the ''.replace()
method like so:
'roses can be blue, red, white'.replace(',' , ',n')
gives 'roses can be blue,n red,n white'
efectively inserting 'n'
after every ,
add a comment |
you can use the ''.replace()
method like so:
'roses can be blue, red, white'.replace(',' , ',n')
gives 'roses can be blue,n red,n white'
efectively inserting 'n'
after every ,
add a comment |
you can use the ''.replace()
method like so:
'roses can be blue, red, white'.replace(',' , ',n')
gives 'roses can be blue,n red,n white'
efectively inserting 'n'
after every ,
you can use the ''.replace()
method like so:
'roses can be blue, red, white'.replace(',' , ',n')
gives 'roses can be blue,n red,n white'
efectively inserting 'n'
after every ,
answered Nov 13 '18 at 20:42
vencaslacvencaslac
1,070317
1,070317
add a comment |
add a comment |
Sounds like a better job for sed (gnu.org/software/sed/manual/sed.html). Something like
sed ‘s/,/,n/’
– cole
Nov 13 '18 at 20:42
Possible duplicate of Split a string by a delimiter in python
– mrk
Nov 13 '18 at 21:13