Remove character from a list made of a data in a file
Well I have a file with this data on it:
- Jesus Rodriguez, 5 E 5.2 1.7
- Luka Zorichk, 21 P 11.2 4.8
- Jorge Campillons, 15 L 7 3.3
And more data.. Each line has a name, the numbers, the position they play, number of points and the ratio of rebounds ("Jesus Rodriguez", "5", "E", "5.2", "1.7").
Okey the problem that I have is when I get the data from the file, and the name is added to the list with a comma and I do not want that character on it. Here's the code that I have for now:
def leeFich(f):
listaDatos = list()
d = open(f)
for lin in d:
listaDatos.append(lin.strip())
for i in listaDatos:
i.replace(',', ' ') // Here is what I have tried to remove the comma
return(listaDatos)
print(leeFich("ligaBaloncesto.txt"))
And the output still has the comma in the first item in the list that I just made.
Output:['Jesus Rodriguez, 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
Expected Output:['Jesus Rodriguez 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
python python-3.x algorithm file arraylist
add a comment |
Well I have a file with this data on it:
- Jesus Rodriguez, 5 E 5.2 1.7
- Luka Zorichk, 21 P 11.2 4.8
- Jorge Campillons, 15 L 7 3.3
And more data.. Each line has a name, the numbers, the position they play, number of points and the ratio of rebounds ("Jesus Rodriguez", "5", "E", "5.2", "1.7").
Okey the problem that I have is when I get the data from the file, and the name is added to the list with a comma and I do not want that character on it. Here's the code that I have for now:
def leeFich(f):
listaDatos = list()
d = open(f)
for lin in d:
listaDatos.append(lin.strip())
for i in listaDatos:
i.replace(',', ' ') // Here is what I have tried to remove the comma
return(listaDatos)
print(leeFich("ligaBaloncesto.txt"))
And the output still has the comma in the first item in the list that I just made.
Output:['Jesus Rodriguez, 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
Expected Output:['Jesus Rodriguez 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
python python-3.x algorithm file arraylist
Can you paste the output in formatted text, rather than images? Also, add expected output.
– Mayank Porwal
Dec 27 '18 at 15:09
Already done! thanks for the tip
– Jesus A. Garrido Cordero
Dec 27 '18 at 15:58
add a comment |
Well I have a file with this data on it:
- Jesus Rodriguez, 5 E 5.2 1.7
- Luka Zorichk, 21 P 11.2 4.8
- Jorge Campillons, 15 L 7 3.3
And more data.. Each line has a name, the numbers, the position they play, number of points and the ratio of rebounds ("Jesus Rodriguez", "5", "E", "5.2", "1.7").
Okey the problem that I have is when I get the data from the file, and the name is added to the list with a comma and I do not want that character on it. Here's the code that I have for now:
def leeFich(f):
listaDatos = list()
d = open(f)
for lin in d:
listaDatos.append(lin.strip())
for i in listaDatos:
i.replace(',', ' ') // Here is what I have tried to remove the comma
return(listaDatos)
print(leeFich("ligaBaloncesto.txt"))
And the output still has the comma in the first item in the list that I just made.
Output:['Jesus Rodriguez, 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
Expected Output:['Jesus Rodriguez 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
python python-3.x algorithm file arraylist
Well I have a file with this data on it:
- Jesus Rodriguez, 5 E 5.2 1.7
- Luka Zorichk, 21 P 11.2 4.8
- Jorge Campillons, 15 L 7 3.3
And more data.. Each line has a name, the numbers, the position they play, number of points and the ratio of rebounds ("Jesus Rodriguez", "5", "E", "5.2", "1.7").
Okey the problem that I have is when I get the data from the file, and the name is added to the list with a comma and I do not want that character on it. Here's the code that I have for now:
def leeFich(f):
listaDatos = list()
d = open(f)
for lin in d:
listaDatos.append(lin.strip())
for i in listaDatos:
i.replace(',', ' ') // Here is what I have tried to remove the comma
return(listaDatos)
print(leeFich("ligaBaloncesto.txt"))
And the output still has the comma in the first item in the list that I just made.
Output:['Jesus Rodriguez, 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
Expected Output:['Jesus Rodriguez 5 E 5.2 1.7', 'Luka Zorichk, 21 P 11.2 4.8', 'Jorge Campillons, 15 L 7 3.3', '']
python python-3.x algorithm file arraylist
python python-3.x algorithm file arraylist
edited Dec 27 '18 at 15:57
asked Dec 27 '18 at 15:02
Jesus A. Garrido Cordero
235
235
Can you paste the output in formatted text, rather than images? Also, add expected output.
– Mayank Porwal
Dec 27 '18 at 15:09
Already done! thanks for the tip
– Jesus A. Garrido Cordero
Dec 27 '18 at 15:58
add a comment |
Can you paste the output in formatted text, rather than images? Also, add expected output.
– Mayank Porwal
Dec 27 '18 at 15:09
Already done! thanks for the tip
– Jesus A. Garrido Cordero
Dec 27 '18 at 15:58
Can you paste the output in formatted text, rather than images? Also, add expected output.
– Mayank Porwal
Dec 27 '18 at 15:09
Can you paste the output in formatted text, rather than images? Also, add expected output.
– Mayank Porwal
Dec 27 '18 at 15:09
Already done! thanks for the tip
– Jesus A. Garrido Cordero
Dec 27 '18 at 15:58
Already done! thanks for the tip
– Jesus A. Garrido Cordero
Dec 27 '18 at 15:58
add a comment |
1 Answer
1
active
oldest
votes
Firstly, str.replace
doesn't work in-place. Besides, you can perform the replacing on few lines above while you strip the line. Secondly, for reading a file object it's better to use with
statement that closes the file at the end of the block and doesn't open the chance of memory leak for you. Thirdly, instead of creating a list and calling the append
attribute on it at each loop you can simple create your list using a list comprehension.
def leeFich(f):
with open(f) as d:
lista_datos = [lin.strip().replace(',', '') for lin in d]
return list_dataos
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
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%2f53947043%2fremove-character-from-a-list-made-of-a-data-in-a-file%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
Firstly, str.replace
doesn't work in-place. Besides, you can perform the replacing on few lines above while you strip the line. Secondly, for reading a file object it's better to use with
statement that closes the file at the end of the block and doesn't open the chance of memory leak for you. Thirdly, instead of creating a list and calling the append
attribute on it at each loop you can simple create your list using a list comprehension.
def leeFich(f):
with open(f) as d:
lista_datos = [lin.strip().replace(',', '') for lin in d]
return list_dataos
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
add a comment |
Firstly, str.replace
doesn't work in-place. Besides, you can perform the replacing on few lines above while you strip the line. Secondly, for reading a file object it's better to use with
statement that closes the file at the end of the block and doesn't open the chance of memory leak for you. Thirdly, instead of creating a list and calling the append
attribute on it at each loop you can simple create your list using a list comprehension.
def leeFich(f):
with open(f) as d:
lista_datos = [lin.strip().replace(',', '') for lin in d]
return list_dataos
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
add a comment |
Firstly, str.replace
doesn't work in-place. Besides, you can perform the replacing on few lines above while you strip the line. Secondly, for reading a file object it's better to use with
statement that closes the file at the end of the block and doesn't open the chance of memory leak for you. Thirdly, instead of creating a list and calling the append
attribute on it at each loop you can simple create your list using a list comprehension.
def leeFich(f):
with open(f) as d:
lista_datos = [lin.strip().replace(',', '') for lin in d]
return list_dataos
Firstly, str.replace
doesn't work in-place. Besides, you can perform the replacing on few lines above while you strip the line. Secondly, for reading a file object it's better to use with
statement that closes the file at the end of the block and doesn't open the chance of memory leak for you. Thirdly, instead of creating a list and calling the append
attribute on it at each loop you can simple create your list using a list comprehension.
def leeFich(f):
with open(f) as d:
lista_datos = [lin.strip().replace(',', '') for lin in d]
return list_dataos
answered Dec 27 '18 at 15:13
Kasrâmvd
77.7k1089124
77.7k1089124
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
add a comment |
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
Perfect! Didn't know it can be done that way! thanks a lot :)
– Jesus A. Garrido Cordero
Dec 27 '18 at 16:03
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%2f53947043%2fremove-character-from-a-list-made-of-a-data-in-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
Can you paste the output in formatted text, rather than images? Also, add expected output.
– Mayank Porwal
Dec 27 '18 at 15:09
Already done! thanks for the tip
– Jesus A. Garrido Cordero
Dec 27 '18 at 15:58