What is the best way in xonsh to loop over the lines of a file?
What is the best way in the xonsh
shell to loop over the lines of a text file?
(A) At the moment I'm using
for l in !(cat file.txt):
line = l.strip()
# Do something with line...
(B) Of course, there is also
with open(p'file.txt') as f:
for l in f:
line = l.strip()
# Do something with line...
I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip()
into the loop?
Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.
python bash xonsh
add a comment |
What is the best way in the xonsh
shell to loop over the lines of a text file?
(A) At the moment I'm using
for l in !(cat file.txt):
line = l.strip()
# Do something with line...
(B) Of course, there is also
with open(p'file.txt') as f:
for l in f:
line = l.strip()
# Do something with line...
I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip()
into the loop?
Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.
python bash xonsh
add a comment |
What is the best way in the xonsh
shell to loop over the lines of a text file?
(A) At the moment I'm using
for l in !(cat file.txt):
line = l.strip()
# Do something with line...
(B) Of course, there is also
with open(p'file.txt') as f:
for l in f:
line = l.strip()
# Do something with line...
I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip()
into the loop?
Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.
python bash xonsh
What is the best way in the xonsh
shell to loop over the lines of a text file?
(A) At the moment I'm using
for l in !(cat file.txt):
line = l.strip()
# Do something with line...
(B) Of course, there is also
with open(p'file.txt') as f:
for l in f:
line = l.strip()
# Do something with line...
I use (A) because it is shorter, but is there anything even more concise? And preferably folding the l.strip()
into the loop?
Note: My main interest is conciseness (in the sense of a small character count) - maybe using xonsh's special syntax features if that helps the cause.
python bash xonsh
python bash xonsh
edited Nov 12 '18 at 2:33
asked Nov 12 '18 at 0:31
halloleo
2,37752258
2,37752258
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can fold str.strip()
into the loop with map()
:
(A):
for l in map(str.strip, !(cat file.txt)):
# Do something with line...
(B):
with open('file.txt') as f:
for l in map(str.strip, f):
# Do something with l..
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
add a comment |
Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:
for l in map(str.strip, open('file.txt')):
# do stuff with l
Or using the p'' string to make a path in xonsh (this does properly close the file):
for l in p'file.txt'.read_text().splitlines():
# do stuff with l
splitlines()
already removes the new line characters, but not other whitespace.
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Just realised that the method onPath
objects is calledread_text
, notget_text
. Edited the answer accordingly.
– halloleo
Nov 14 '18 at 0:58
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254629%2fwhat-is-the-best-way-in-xonsh-to-loop-over-the-lines-of-a-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can fold str.strip()
into the loop with map()
:
(A):
for l in map(str.strip, !(cat file.txt)):
# Do something with line...
(B):
with open('file.txt') as f:
for l in map(str.strip, f):
# Do something with l..
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
add a comment |
You can fold str.strip()
into the loop with map()
:
(A):
for l in map(str.strip, !(cat file.txt)):
# Do something with line...
(B):
with open('file.txt') as f:
for l in map(str.strip, f):
# Do something with l..
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
add a comment |
You can fold str.strip()
into the loop with map()
:
(A):
for l in map(str.strip, !(cat file.txt)):
# Do something with line...
(B):
with open('file.txt') as f:
for l in map(str.strip, f):
# Do something with l..
You can fold str.strip()
into the loop with map()
:
(A):
for l in map(str.strip, !(cat file.txt)):
# Do something with line...
(B):
with open('file.txt') as f:
for l in map(str.strip, f):
# Do something with l..
edited Nov 12 '18 at 1:30
halloleo
2,37752258
2,37752258
answered Nov 12 '18 at 0:55
RoadRunner
10.7k31340
10.7k31340
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
add a comment |
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
Yes, both versions work. I have just tested them in xonsh 0.8.3.
– halloleo
Nov 12 '18 at 2:39
add a comment |
Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:
for l in map(str.strip, open('file.txt')):
# do stuff with l
Or using the p'' string to make a path in xonsh (this does properly close the file):
for l in p'file.txt'.read_text().splitlines():
# do stuff with l
splitlines()
already removes the new line characters, but not other whitespace.
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Just realised that the method onPath
objects is calledread_text
, notget_text
. Edited the answer accordingly.
– halloleo
Nov 14 '18 at 0:58
|
show 1 more comment
Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:
for l in map(str.strip, open('file.txt')):
# do stuff with l
Or using the p'' string to make a path in xonsh (this does properly close the file):
for l in p'file.txt'.read_text().splitlines():
# do stuff with l
splitlines()
already removes the new line characters, but not other whitespace.
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Just realised that the method onPath
objects is calledread_text
, notget_text
. Edited the answer accordingly.
– halloleo
Nov 14 '18 at 0:58
|
show 1 more comment
Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:
for l in map(str.strip, open('file.txt')):
# do stuff with l
Or using the p'' string to make a path in xonsh (this does properly close the file):
for l in p'file.txt'.read_text().splitlines():
# do stuff with l
splitlines()
already removes the new line characters, but not other whitespace.
Minimal character count could even involve relying on your python implementation to release the file at the end of execution, rather than doing it explicitly:
for l in map(str.strip, open('file.txt')):
# do stuff with l
Or using the p'' string to make a path in xonsh (this does properly close the file):
for l in p'file.txt'.read_text().splitlines():
# do stuff with l
splitlines()
already removes the new line characters, but not other whitespace.
edited Nov 14 '18 at 0:56
halloleo
2,37752258
2,37752258
answered Nov 12 '18 at 1:27
soundstripe
48138
48138
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Just realised that the method onPath
objects is calledread_text
, notget_text
. Edited the answer accordingly.
– halloleo
Nov 14 '18 at 0:58
|
show 1 more comment
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Just realised that the method onPath
objects is calledread_text
, notget_text
. Edited the answer accordingly.
– halloleo
Nov 14 '18 at 0:58
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
I wouldn't say this is in any way more concise than the code in the question...
– halloleo
Nov 12 '18 at 2:29
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
True...I misinterpreted your need for conciseness as a need for readability.
– soundstripe
Nov 12 '18 at 2:59
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Edited my response for brevity in code. Note that CPython will auto-close your file at program exit but other Python implementations may not.
– soundstripe
Nov 12 '18 at 14:34
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Wow, the version with the p'' notation looks now pretty compelling to me. Thx.
– halloleo
Nov 13 '18 at 11:18
Just realised that the method on
Path
objects is called read_text
, not get_text
. Edited the answer accordingly.– halloleo
Nov 14 '18 at 0:58
Just realised that the method on
Path
objects is called read_text
, not get_text
. Edited the answer accordingly.– halloleo
Nov 14 '18 at 0:58
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254629%2fwhat-is-the-best-way-in-xonsh-to-loop-over-the-lines-of-a-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown