Order a text file in alphabetical order using the second column in Python
I have tried all kinds of ways to get the text file to order alphabetically by the last name. The last name is currently read as column two of each split line. I can sort by column one without a problem. I try to put last.sort() or use the sorted(myList) but it doesn't work. I have even tried importing itemgetter. Please help! Pictures of results... previous results with column one sorted... Picture of Text File
TXT FILE INFO:
654,Jones,1,18:03
733,Smith,3,18:09
394,Jackson,4,18:22
876,Cole,1,18:23
555,Cruz,5,18:28
741,Martinez,2,18:33
499,Davis,2,18:36
338,Blunt,3,18:44
632,Patton,5,18:45
712,Joyce,4,18:49
112,Shoemaker,1,18:55
321,Smart,5,18:58
564,Love,2,19:01
843,Grove,4,19:05
933,Ham,3,19:10
with open("Race_Results_Sample.txt", "r")as myList:
myList= myList.read().split()
sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line.split(",")
print num, last, org, time
python python-3.x python-2.7 text-files alphabetical-sort
add a comment |
I have tried all kinds of ways to get the text file to order alphabetically by the last name. The last name is currently read as column two of each split line. I can sort by column one without a problem. I try to put last.sort() or use the sorted(myList) but it doesn't work. I have even tried importing itemgetter. Please help! Pictures of results... previous results with column one sorted... Picture of Text File
TXT FILE INFO:
654,Jones,1,18:03
733,Smith,3,18:09
394,Jackson,4,18:22
876,Cole,1,18:23
555,Cruz,5,18:28
741,Martinez,2,18:33
499,Davis,2,18:36
338,Blunt,3,18:44
632,Patton,5,18:45
712,Joyce,4,18:49
112,Shoemaker,1,18:55
321,Smart,5,18:58
564,Love,2,19:01
843,Grove,4,19:05
933,Ham,3,19:10
with open("Race_Results_Sample.txt", "r")as myList:
myList= myList.read().split()
sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line.split(",")
print num, last, org, time
python python-3.x python-2.7 text-files alphabetical-sort
1
Can you include a sample of the first ten lines or so of your text file?
– rahlf23
Nov 19 at 22:00
You are originally splitting by spaces and later splitting by comma. You probably need to do the same type of split in each case. Try changing.split()to.split(',').
– forgetso
Nov 19 at 22:12
I have tried to change the first .split() to include the ',' but it errors: IndexError: string index out of range
– Shandie Tucker
Nov 19 at 22:24
add a comment |
I have tried all kinds of ways to get the text file to order alphabetically by the last name. The last name is currently read as column two of each split line. I can sort by column one without a problem. I try to put last.sort() or use the sorted(myList) but it doesn't work. I have even tried importing itemgetter. Please help! Pictures of results... previous results with column one sorted... Picture of Text File
TXT FILE INFO:
654,Jones,1,18:03
733,Smith,3,18:09
394,Jackson,4,18:22
876,Cole,1,18:23
555,Cruz,5,18:28
741,Martinez,2,18:33
499,Davis,2,18:36
338,Blunt,3,18:44
632,Patton,5,18:45
712,Joyce,4,18:49
112,Shoemaker,1,18:55
321,Smart,5,18:58
564,Love,2,19:01
843,Grove,4,19:05
933,Ham,3,19:10
with open("Race_Results_Sample.txt", "r")as myList:
myList= myList.read().split()
sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line.split(",")
print num, last, org, time
python python-3.x python-2.7 text-files alphabetical-sort
I have tried all kinds of ways to get the text file to order alphabetically by the last name. The last name is currently read as column two of each split line. I can sort by column one without a problem. I try to put last.sort() or use the sorted(myList) but it doesn't work. I have even tried importing itemgetter. Please help! Pictures of results... previous results with column one sorted... Picture of Text File
TXT FILE INFO:
654,Jones,1,18:03
733,Smith,3,18:09
394,Jackson,4,18:22
876,Cole,1,18:23
555,Cruz,5,18:28
741,Martinez,2,18:33
499,Davis,2,18:36
338,Blunt,3,18:44
632,Patton,5,18:45
712,Joyce,4,18:49
112,Shoemaker,1,18:55
321,Smart,5,18:58
564,Love,2,19:01
843,Grove,4,19:05
933,Ham,3,19:10
with open("Race_Results_Sample.txt", "r")as myList:
myList= myList.read().split()
sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line.split(",")
print num, last, org, time
python python-3.x python-2.7 text-files alphabetical-sort
python python-3.x python-2.7 text-files alphabetical-sort
edited Nov 19 at 22:21
asked Nov 19 at 21:57
Shandie Tucker
203
203
1
Can you include a sample of the first ten lines or so of your text file?
– rahlf23
Nov 19 at 22:00
You are originally splitting by spaces and later splitting by comma. You probably need to do the same type of split in each case. Try changing.split()to.split(',').
– forgetso
Nov 19 at 22:12
I have tried to change the first .split() to include the ',' but it errors: IndexError: string index out of range
– Shandie Tucker
Nov 19 at 22:24
add a comment |
1
Can you include a sample of the first ten lines or so of your text file?
– rahlf23
Nov 19 at 22:00
You are originally splitting by spaces and later splitting by comma. You probably need to do the same type of split in each case. Try changing.split()to.split(',').
– forgetso
Nov 19 at 22:12
I have tried to change the first .split() to include the ',' but it errors: IndexError: string index out of range
– Shandie Tucker
Nov 19 at 22:24
1
1
Can you include a sample of the first ten lines or so of your text file?
– rahlf23
Nov 19 at 22:00
Can you include a sample of the first ten lines or so of your text file?
– rahlf23
Nov 19 at 22:00
You are originally splitting by spaces and later splitting by comma. You probably need to do the same type of split in each case. Try changing
.split() to .split(',').– forgetso
Nov 19 at 22:12
You are originally splitting by spaces and later splitting by comma. You probably need to do the same type of split in each case. Try changing
.split() to .split(',').– forgetso
Nov 19 at 22:12
I have tried to change the first .split() to include the ',' but it errors: IndexError: string index out of range
– Shandie Tucker
Nov 19 at 22:24
I have tried to change the first .split() to include the ',' but it errors: IndexError: string index out of range
– Shandie Tucker
Nov 19 at 22:24
add a comment |
1 Answer
1
active
oldest
votes
Try parsing the text into a 2d List, something like
with open("test.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
print num, last, org, time
Split per linebreak and then again for each line per comma.
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
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%2f53383224%2forder-a-text-file-in-alphabetical-order-using-the-second-column-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try parsing the text into a 2d List, something like
with open("test.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
print num, last, org, time
Split per linebreak and then again for each line per comma.
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
add a comment |
Try parsing the text into a 2d List, something like
with open("test.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
print num, last, org, time
Split per linebreak and then again for each line per comma.
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
add a comment |
Try parsing the text into a 2d List, something like
with open("test.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
print num, last, org, time
Split per linebreak and then again for each line per comma.
Try parsing the text into a 2d List, something like
with open("test.txt", "r")as myList:
myList = myList.read()
myList = [l.split(",") for l in myList.splitlines()]
myList = sorted(myList, key=lambda kv: kv[1])
for line in myList:
num, last, org, time = line
print num, last, org, time
Split per linebreak and then again for each line per comma.
answered Nov 19 at 22:27
pxe
1207
1207
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
add a comment |
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
This works perfectly!!! Thank you so very much!! I have been trying all day to get this to work.
– Shandie Tucker
Nov 19 at 22:31
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.
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%2f53383224%2forder-a-text-file-in-alphabetical-order-using-the-second-column-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
1
Can you include a sample of the first ten lines or so of your text file?
– rahlf23
Nov 19 at 22:00
You are originally splitting by spaces and later splitting by comma. You probably need to do the same type of split in each case. Try changing
.split()to.split(',').– forgetso
Nov 19 at 22:12
I have tried to change the first .split() to include the ',' but it errors: IndexError: string index out of range
– Shandie Tucker
Nov 19 at 22:24