How to limit the length of each line in a text file using python [duplicate]
This question already has an answer here:
set a limit to the number of words or characters in a string
3 answers
I have a text file of following type-
eng Firstly, in the course of the last few decades the national
eng Secondly, the national courts will be empowered to implement
eng However, I am convinced of the fact that the White Paper has put us on
the right path.
I want to restrict the length of each line upto (say) 9 words.
I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?
Sample Output-
eng Firstly, in the course of the last few
eng Secondly, the national courts will be empowered to
eng However, I am convinced of the fact that
python python-3.x
marked as duplicate by MSeifert, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
set a limit to the number of words or characters in a string
3 answers
I have a text file of following type-
eng Firstly, in the course of the last few decades the national
eng Secondly, the national courts will be empowered to implement
eng However, I am convinced of the fact that the White Paper has put us on
the right path.
I want to restrict the length of each line upto (say) 9 words.
I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?
Sample Output-
eng Firstly, in the course of the last few
eng Secondly, the national courts will be empowered to
eng However, I am convinced of the fact that
python python-3.x
marked as duplicate by MSeifert, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
set a limit to the number of words or characters in a string
3 answers
I have a text file of following type-
eng Firstly, in the course of the last few decades the national
eng Secondly, the national courts will be empowered to implement
eng However, I am convinced of the fact that the White Paper has put us on
the right path.
I want to restrict the length of each line upto (say) 9 words.
I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?
Sample Output-
eng Firstly, in the course of the last few
eng Secondly, the national courts will be empowered to
eng However, I am convinced of the fact that
python python-3.x
This question already has an answer here:
set a limit to the number of words or characters in a string
3 answers
I have a text file of following type-
eng Firstly, in the course of the last few decades the national
eng Secondly, the national courts will be empowered to implement
eng However, I am convinced of the fact that the White Paper has put us on
the right path.
I want to restrict the length of each line upto (say) 9 words.
I tried of using python's read_line method but it specifies the size of the line only.I am unable to find any other suitable method. How to do it ?
Sample Output-
eng Firstly, in the course of the last few
eng Secondly, the national courts will be empowered to
eng However, I am convinced of the fact that
This question already has an answer here:
set a limit to the number of words or characters in a string
3 answers
python python-3.x
python python-3.x
asked Nov 12 '18 at 19:11
Bing
86112
86112
marked as duplicate by MSeifert, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by MSeifert, Martijn Pieters♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
To get the first n words of a string as a string:
def first_n_words(s, n):
return ' '.join(s.split()[:n])
add a comment |
You could make a list of each word like so:
with open(file, 'r') as f:
lines =
for line in f:
lines.append(line.rstrip('n').split())
Now limit each line to 9 by using slicing which truncated automatically:
with open(file, 'w') as f:
for line in lines:
f.write(' '.join(line[:9]))
f.write('n')
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To get the first n words of a string as a string:
def first_n_words(s, n):
return ' '.join(s.split()[:n])
add a comment |
To get the first n words of a string as a string:
def first_n_words(s, n):
return ' '.join(s.split()[:n])
add a comment |
To get the first n words of a string as a string:
def first_n_words(s, n):
return ' '.join(s.split()[:n])
To get the first n words of a string as a string:
def first_n_words(s, n):
return ' '.join(s.split()[:n])
answered Nov 12 '18 at 19:14
MegaBluejay
35118
35118
add a comment |
add a comment |
You could make a list of each word like so:
with open(file, 'r') as f:
lines =
for line in f:
lines.append(line.rstrip('n').split())
Now limit each line to 9 by using slicing which truncated automatically:
with open(file, 'w') as f:
for line in lines:
f.write(' '.join(line[:9]))
f.write('n')
add a comment |
You could make a list of each word like so:
with open(file, 'r') as f:
lines =
for line in f:
lines.append(line.rstrip('n').split())
Now limit each line to 9 by using slicing which truncated automatically:
with open(file, 'w') as f:
for line in lines:
f.write(' '.join(line[:9]))
f.write('n')
add a comment |
You could make a list of each word like so:
with open(file, 'r') as f:
lines =
for line in f:
lines.append(line.rstrip('n').split())
Now limit each line to 9 by using slicing which truncated automatically:
with open(file, 'w') as f:
for line in lines:
f.write(' '.join(line[:9]))
f.write('n')
You could make a list of each word like so:
with open(file, 'r') as f:
lines =
for line in f:
lines.append(line.rstrip('n').split())
Now limit each line to 9 by using slicing which truncated automatically:
with open(file, 'w') as f:
for line in lines:
f.write(' '.join(line[:9]))
f.write('n')
answered Nov 12 '18 at 19:20
N Chauhan
1,6941214
1,6941214
add a comment |
add a comment |