Better way to extract only 2nd column of a txt file in python?
I have a txt file that looks like this. The number part and sentence part is separated by one tab.
1234 I'll give 5.
1334 Surprisingly well made.
I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split()
and .join()
as below
f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)
Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?
python python-3.x file-io
add a comment |
I have a txt file that looks like this. The number part and sentence part is separated by one tab.
1234 I'll give 5.
1334 Surprisingly well made.
I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split()
and .join()
as below
f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)
Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?
python python-3.x file-io
Why not justline = line.split()[1]
?
– slider
Nov 19 '18 at 2:14
.split('t', 1)
?
– Klaus D.
Nov 19 '18 at 2:15
@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25
@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26
add a comment |
I have a txt file that looks like this. The number part and sentence part is separated by one tab.
1234 I'll give 5.
1334 Surprisingly well made.
I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split()
and .join()
as below
f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)
Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?
python python-3.x file-io
I have a txt file that looks like this. The number part and sentence part is separated by one tab.
1234 I'll give 5.
1334 Surprisingly well made.
I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split()
and .join()
as below
f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)
Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?
python python-3.x file-io
python python-3.x file-io
asked Nov 19 '18 at 2:11
PuffedRiceCrackersPuffedRiceCrackers
536
536
Why not justline = line.split()[1]
?
– slider
Nov 19 '18 at 2:14
.split('t', 1)
?
– Klaus D.
Nov 19 '18 at 2:15
@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25
@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26
add a comment |
Why not justline = line.split()[1]
?
– slider
Nov 19 '18 at 2:14
.split('t', 1)
?
– Klaus D.
Nov 19 '18 at 2:15
@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25
@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26
Why not just
line = line.split()[1]
?– slider
Nov 19 '18 at 2:14
Why not just
line = line.split()[1]
?– slider
Nov 19 '18 at 2:14
.split('t', 1)
?– Klaus D.
Nov 19 '18 at 2:15
.split('t', 1)
?– Klaus D.
Nov 19 '18 at 2:15
@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25
@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25
@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26
@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26
add a comment |
3 Answers
3
active
oldest
votes
Or kinda easier:
with open('test.txt', 'r', encoding='utf8') as f:
print('n'.join(line.split()[1] for line in f))
1
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
add a comment |
You could slice and use a context manager so your file handler is not orphaned in edge cases.
with open('test.txt', 'r', encoding='utf8') as f
for line in f:
print((' '.join(line.split()[1:]))
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
add a comment |
Assuming each line always has the second part:
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
print(line.split('t', 1)[1])
If it does not (then skip it):
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
try:
print(line.split('t', 1)[1])
except IndexError:
pass
add a 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%2f53367424%2fbetter-way-to-extract-only-2nd-column-of-a-txt-file-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Or kinda easier:
with open('test.txt', 'r', encoding='utf8') as f:
print('n'.join(line.split()[1] for line in f))
1
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
add a comment |
Or kinda easier:
with open('test.txt', 'r', encoding='utf8') as f:
print('n'.join(line.split()[1] for line in f))
1
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
add a comment |
Or kinda easier:
with open('test.txt', 'r', encoding='utf8') as f:
print('n'.join(line.split()[1] for line in f))
Or kinda easier:
with open('test.txt', 'r', encoding='utf8') as f:
print('n'.join(line.split()[1] for line in f))
answered Nov 19 '18 at 2:25
U9-ForwardU9-Forward
13.6k21337
13.6k21337
1
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
add a comment |
1
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
1
1
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
This is the best answer, but I'll leave mine up as reference
– Charles Landau
Nov 19 '18 at 2:26
add a comment |
You could slice and use a context manager so your file handler is not orphaned in edge cases.
with open('test.txt', 'r', encoding='utf8') as f
for line in f:
print((' '.join(line.split()[1:]))
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
add a comment |
You could slice and use a context manager so your file handler is not orphaned in edge cases.
with open('test.txt', 'r', encoding='utf8') as f
for line in f:
print((' '.join(line.split()[1:]))
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
add a comment |
You could slice and use a context manager so your file handler is not orphaned in edge cases.
with open('test.txt', 'r', encoding='utf8') as f
for line in f:
print((' '.join(line.split()[1:]))
You could slice and use a context manager so your file handler is not orphaned in edge cases.
with open('test.txt', 'r', encoding='utf8') as f
for line in f:
print((' '.join(line.split()[1:]))
answered Nov 19 '18 at 2:15
Charles LandauCharles Landau
2,0651215
2,0651215
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
add a comment |
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
– PuffedRiceCrackers
Nov 19 '18 at 2:29
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
– Charles Landau
Nov 19 '18 at 2:30
add a comment |
Assuming each line always has the second part:
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
print(line.split('t', 1)[1])
If it does not (then skip it):
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
try:
print(line.split('t', 1)[1])
except IndexError:
pass
add a comment |
Assuming each line always has the second part:
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
print(line.split('t', 1)[1])
If it does not (then skip it):
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
try:
print(line.split('t', 1)[1])
except IndexError:
pass
add a comment |
Assuming each line always has the second part:
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
print(line.split('t', 1)[1])
If it does not (then skip it):
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
try:
print(line.split('t', 1)[1])
except IndexError:
pass
Assuming each line always has the second part:
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
print(line.split('t', 1)[1])
If it does not (then skip it):
with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
try:
print(line.split('t', 1)[1])
except IndexError:
pass
answered Nov 19 '18 at 2:30
DYZDYZ
25.8k61948
25.8k61948
add a comment |
add a 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.
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%2f53367424%2fbetter-way-to-extract-only-2nd-column-of-a-txt-file-in-python%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
Why not just
line = line.split()[1]
?– slider
Nov 19 '18 at 2:14
.split('t', 1)
?– Klaus D.
Nov 19 '18 at 2:15
@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25
@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26