Sorting .txt files
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am trying to make the 'scores' section of my code sort it self in ascending order, and the print the 5 highest values. I've tried using panda but it doesn't help. can anyone help?
x = 0
Name = [None]*1000
Score = [0]*100
text_file = open("txt.Scores.txt","a")
def write_in_file():
global text_file
text_file.write(Name[x])
text_file.write("n")
text_file.write(Score[x])
text_file.write("n")
text_file.write("n")
text_file.close()
text_file = open("txt.Scores.txt","a")
for i in name:
Name[x] = input("Username:")
Score[x] = input("Score:")
write_in_file()
print(Score)
x += 1
when i run the code, it gives me an output that looks like this:
Username:Jonny
Score:15
['15', 0, 0, 0, 0]
Username:Max
Score:15
['15', '15', 0, 0, 0, 0]
and the text file looks like this:
Jonny
15
Max
15
I am trying to get it to print the 5 highest scores only
python
add a comment |
I am trying to make the 'scores' section of my code sort it self in ascending order, and the print the 5 highest values. I've tried using panda but it doesn't help. can anyone help?
x = 0
Name = [None]*1000
Score = [0]*100
text_file = open("txt.Scores.txt","a")
def write_in_file():
global text_file
text_file.write(Name[x])
text_file.write("n")
text_file.write(Score[x])
text_file.write("n")
text_file.write("n")
text_file.close()
text_file = open("txt.Scores.txt","a")
for i in name:
Name[x] = input("Username:")
Score[x] = input("Score:")
write_in_file()
print(Score)
x += 1
when i run the code, it gives me an output that looks like this:
Username:Jonny
Score:15
['15', 0, 0, 0, 0]
Username:Max
Score:15
['15', '15', 0, 0, 0, 0]
and the text file looks like this:
Jonny
15
Max
15
I am trying to get it to print the 5 highest scores only
python
Please share a sample of your text file which you are reading and also the expected output.
– Mayank Porwal
Nov 23 '18 at 11:31
You don't sort anywhere, you are just writing out empty strings and zeroes.
– Bart Friederichs
Nov 23 '18 at 11:33
your code currently has no logic written to "Sort" anything as far as i can tell. try to search around for "how to sort a list". also, look at the pythonic way to open a file. additionally, do not modify the same list you are iterating through.
– Paritosh Singh
Nov 23 '18 at 11:40
add a comment |
I am trying to make the 'scores' section of my code sort it self in ascending order, and the print the 5 highest values. I've tried using panda but it doesn't help. can anyone help?
x = 0
Name = [None]*1000
Score = [0]*100
text_file = open("txt.Scores.txt","a")
def write_in_file():
global text_file
text_file.write(Name[x])
text_file.write("n")
text_file.write(Score[x])
text_file.write("n")
text_file.write("n")
text_file.close()
text_file = open("txt.Scores.txt","a")
for i in name:
Name[x] = input("Username:")
Score[x] = input("Score:")
write_in_file()
print(Score)
x += 1
when i run the code, it gives me an output that looks like this:
Username:Jonny
Score:15
['15', 0, 0, 0, 0]
Username:Max
Score:15
['15', '15', 0, 0, 0, 0]
and the text file looks like this:
Jonny
15
Max
15
I am trying to get it to print the 5 highest scores only
python
I am trying to make the 'scores' section of my code sort it self in ascending order, and the print the 5 highest values. I've tried using panda but it doesn't help. can anyone help?
x = 0
Name = [None]*1000
Score = [0]*100
text_file = open("txt.Scores.txt","a")
def write_in_file():
global text_file
text_file.write(Name[x])
text_file.write("n")
text_file.write(Score[x])
text_file.write("n")
text_file.write("n")
text_file.close()
text_file = open("txt.Scores.txt","a")
for i in name:
Name[x] = input("Username:")
Score[x] = input("Score:")
write_in_file()
print(Score)
x += 1
when i run the code, it gives me an output that looks like this:
Username:Jonny
Score:15
['15', 0, 0, 0, 0]
Username:Max
Score:15
['15', '15', 0, 0, 0, 0]
and the text file looks like this:
Jonny
15
Max
15
I am trying to get it to print the 5 highest scores only
python
python
edited Nov 23 '18 at 11:39
BigdaddyRudolf4567
asked Nov 23 '18 at 11:21
BigdaddyRudolf4567BigdaddyRudolf4567
83
83
Please share a sample of your text file which you are reading and also the expected output.
– Mayank Porwal
Nov 23 '18 at 11:31
You don't sort anywhere, you are just writing out empty strings and zeroes.
– Bart Friederichs
Nov 23 '18 at 11:33
your code currently has no logic written to "Sort" anything as far as i can tell. try to search around for "how to sort a list". also, look at the pythonic way to open a file. additionally, do not modify the same list you are iterating through.
– Paritosh Singh
Nov 23 '18 at 11:40
add a comment |
Please share a sample of your text file which you are reading and also the expected output.
– Mayank Porwal
Nov 23 '18 at 11:31
You don't sort anywhere, you are just writing out empty strings and zeroes.
– Bart Friederichs
Nov 23 '18 at 11:33
your code currently has no logic written to "Sort" anything as far as i can tell. try to search around for "how to sort a list". also, look at the pythonic way to open a file. additionally, do not modify the same list you are iterating through.
– Paritosh Singh
Nov 23 '18 at 11:40
Please share a sample of your text file which you are reading and also the expected output.
– Mayank Porwal
Nov 23 '18 at 11:31
Please share a sample of your text file which you are reading and also the expected output.
– Mayank Porwal
Nov 23 '18 at 11:31
You don't sort anywhere, you are just writing out empty strings and zeroes.
– Bart Friederichs
Nov 23 '18 at 11:33
You don't sort anywhere, you are just writing out empty strings and zeroes.
– Bart Friederichs
Nov 23 '18 at 11:33
your code currently has no logic written to "Sort" anything as far as i can tell. try to search around for "how to sort a list". also, look at the pythonic way to open a file. additionally, do not modify the same list you are iterating through.
– Paritosh Singh
Nov 23 '18 at 11:40
your code currently has no logic written to "Sort" anything as far as i can tell. try to search around for "how to sort a list". also, look at the pythonic way to open a file. additionally, do not modify the same list you are iterating through.
– Paritosh Singh
Nov 23 '18 at 11:40
add a comment |
3 Answers
3
active
oldest
votes
You can use a dictionary to store Name and its corresponding score, and print the highest score values.
X = {}
with open("txt.Scores.txt", 'w+') as score:
while len(X) < 10:
Name = input("Username: ")
Score = int(input("Score: "))
X[Score] = Name
print(Name + 'tt' + str(Score), file=score)
Y = sorted(list(X.keys()), reverse=True)
y = range(0, len(Y))
Scores = [Y[i] for i in y[:5]]
print(Scores)
print([X[i] for i in Scores])
This code will get 10 names and scores as input, store them in 'txt.Scores.txt' and print 5 highest scores stored in a list, and their corresponding names in another list.
Let me know if it worked for you.
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
add a comment |
you have not defined name which you use in your for loop,you should use while True:instead, then make an empty list name to fill it depending on the input.
add a comment |
At first read about file's descriptors If you don’t use “with”, when does Python close files? The answer is: It depends.
You can use python's sort as:
with open('txt.Scores.txt', 'r') as f:
data = f.readlines()
data = [int(line.strip()) for line in data if line.strip() != str() and all(i.isdigit() for i in line.strip())]
print sorted(data, reverse=True)
Or GNU sort ass executed command with specific keys.
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%2f53445767%2fsorting-txt-files%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
You can use a dictionary to store Name and its corresponding score, and print the highest score values.
X = {}
with open("txt.Scores.txt", 'w+') as score:
while len(X) < 10:
Name = input("Username: ")
Score = int(input("Score: "))
X[Score] = Name
print(Name + 'tt' + str(Score), file=score)
Y = sorted(list(X.keys()), reverse=True)
y = range(0, len(Y))
Scores = [Y[i] for i in y[:5]]
print(Scores)
print([X[i] for i in Scores])
This code will get 10 names and scores as input, store them in 'txt.Scores.txt' and print 5 highest scores stored in a list, and their corresponding names in another list.
Let me know if it worked for you.
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
add a comment |
You can use a dictionary to store Name and its corresponding score, and print the highest score values.
X = {}
with open("txt.Scores.txt", 'w+') as score:
while len(X) < 10:
Name = input("Username: ")
Score = int(input("Score: "))
X[Score] = Name
print(Name + 'tt' + str(Score), file=score)
Y = sorted(list(X.keys()), reverse=True)
y = range(0, len(Y))
Scores = [Y[i] for i in y[:5]]
print(Scores)
print([X[i] for i in Scores])
This code will get 10 names and scores as input, store them in 'txt.Scores.txt' and print 5 highest scores stored in a list, and their corresponding names in another list.
Let me know if it worked for you.
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
add a comment |
You can use a dictionary to store Name and its corresponding score, and print the highest score values.
X = {}
with open("txt.Scores.txt", 'w+') as score:
while len(X) < 10:
Name = input("Username: ")
Score = int(input("Score: "))
X[Score] = Name
print(Name + 'tt' + str(Score), file=score)
Y = sorted(list(X.keys()), reverse=True)
y = range(0, len(Y))
Scores = [Y[i] for i in y[:5]]
print(Scores)
print([X[i] for i in Scores])
This code will get 10 names and scores as input, store them in 'txt.Scores.txt' and print 5 highest scores stored in a list, and their corresponding names in another list.
Let me know if it worked for you.
You can use a dictionary to store Name and its corresponding score, and print the highest score values.
X = {}
with open("txt.Scores.txt", 'w+') as score:
while len(X) < 10:
Name = input("Username: ")
Score = int(input("Score: "))
X[Score] = Name
print(Name + 'tt' + str(Score), file=score)
Y = sorted(list(X.keys()), reverse=True)
y = range(0, len(Y))
Scores = [Y[i] for i in y[:5]]
print(Scores)
print([X[i] for i in Scores])
This code will get 10 names and scores as input, store them in 'txt.Scores.txt' and print 5 highest scores stored in a list, and their corresponding names in another list.
Let me know if it worked for you.
answered Nov 23 '18 at 12:57
Preetkaran SinghPreetkaran Singh
114111
114111
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
add a comment |
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Thank you so much, it worked perfectly
– BigdaddyRudolf4567
Dec 11 '18 at 11:33
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
Accept the answer if it worked for you, so that the question doesn't remain unanswered
– Preetkaran Singh
Dec 11 '18 at 11:47
add a comment |
you have not defined name which you use in your for loop,you should use while True:instead, then make an empty list name to fill it depending on the input.
add a comment |
you have not defined name which you use in your for loop,you should use while True:instead, then make an empty list name to fill it depending on the input.
add a comment |
you have not defined name which you use in your for loop,you should use while True:instead, then make an empty list name to fill it depending on the input.
you have not defined name which you use in your for loop,you should use while True:instead, then make an empty list name to fill it depending on the input.
answered Nov 23 '18 at 11:36
wishmasterwishmaster
32710
32710
add a comment |
add a comment |
At first read about file's descriptors If you don’t use “with”, when does Python close files? The answer is: It depends.
You can use python's sort as:
with open('txt.Scores.txt', 'r') as f:
data = f.readlines()
data = [int(line.strip()) for line in data if line.strip() != str() and all(i.isdigit() for i in line.strip())]
print sorted(data, reverse=True)
Or GNU sort ass executed command with specific keys.
add a comment |
At first read about file's descriptors If you don’t use “with”, when does Python close files? The answer is: It depends.
You can use python's sort as:
with open('txt.Scores.txt', 'r') as f:
data = f.readlines()
data = [int(line.strip()) for line in data if line.strip() != str() and all(i.isdigit() for i in line.strip())]
print sorted(data, reverse=True)
Or GNU sort ass executed command with specific keys.
add a comment |
At first read about file's descriptors If you don’t use “with”, when does Python close files? The answer is: It depends.
You can use python's sort as:
with open('txt.Scores.txt', 'r') as f:
data = f.readlines()
data = [int(line.strip()) for line in data if line.strip() != str() and all(i.isdigit() for i in line.strip())]
print sorted(data, reverse=True)
Or GNU sort ass executed command with specific keys.
At first read about file's descriptors If you don’t use “with”, when does Python close files? The answer is: It depends.
You can use python's sort as:
with open('txt.Scores.txt', 'r') as f:
data = f.readlines()
data = [int(line.strip()) for line in data if line.strip() != str() and all(i.isdigit() for i in line.strip())]
print sorted(data, reverse=True)
Or GNU sort ass executed command with specific keys.
edited Nov 23 '18 at 12:13
answered Nov 23 '18 at 11:38
Ihor VoroninIhor Voronin
1857
1857
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%2f53445767%2fsorting-txt-files%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
Please share a sample of your text file which you are reading and also the expected output.
– Mayank Porwal
Nov 23 '18 at 11:31
You don't sort anywhere, you are just writing out empty strings and zeroes.
– Bart Friederichs
Nov 23 '18 at 11:33
your code currently has no logic written to "Sort" anything as far as i can tell. try to search around for "how to sort a list". also, look at the pythonic way to open a file. additionally, do not modify the same list you are iterating through.
– Paritosh Singh
Nov 23 '18 at 11:40