Python add ', ' to string and return:
fd = open(nom_fichier, 'r')
liste_chaine = fd.readlines()
liste_chaine2 =
for item in liste_chaine:
if item not in "'noirn','blancn','Humainn', 'Ordinateurn', 'Falsen', 'Truen":
liste_chaine2.append(item)
liste_chaine2 = [i.replace('n', '') for i in liste_chaine2]
return liste_chaine2
['3,3,blanc', '3,4,noir', '4,3,noir', '4,4,blanc']
i am reading a file and trying to return a string output exactly like :
3,3,blanc
4,3,noir
3,4,white
i cleaned the file with the code above but need to clean up this list to the required output
python-3.x
add a comment |
fd = open(nom_fichier, 'r')
liste_chaine = fd.readlines()
liste_chaine2 =
for item in liste_chaine:
if item not in "'noirn','blancn','Humainn', 'Ordinateurn', 'Falsen', 'Truen":
liste_chaine2.append(item)
liste_chaine2 = [i.replace('n', '') for i in liste_chaine2]
return liste_chaine2
['3,3,blanc', '3,4,noir', '4,3,noir', '4,4,blanc']
i am reading a file and trying to return a string output exactly like :
3,3,blanc
4,3,noir
3,4,white
i cleaned the file with the code above but need to clean up this list to the required output
python-3.x
sorry its returning
– RIchard Robitaille
Nov 20 at 22:11
you probably used"".join
instead of",".join
. Show your code
– Jean-François Fabre
Nov 20 at 22:14
It seems you have a problem with your code. However, we can't help unless we have code or information that can reproduce the problem. Otherwise, we are just blindly guessing.
– FrankerZ
Nov 20 at 22:18
I have modified and added much more info as requested, if you need more let me know.
– RIchard Robitaille
Nov 20 at 23:44
add a comment |
fd = open(nom_fichier, 'r')
liste_chaine = fd.readlines()
liste_chaine2 =
for item in liste_chaine:
if item not in "'noirn','blancn','Humainn', 'Ordinateurn', 'Falsen', 'Truen":
liste_chaine2.append(item)
liste_chaine2 = [i.replace('n', '') for i in liste_chaine2]
return liste_chaine2
['3,3,blanc', '3,4,noir', '4,3,noir', '4,4,blanc']
i am reading a file and trying to return a string output exactly like :
3,3,blanc
4,3,noir
3,4,white
i cleaned the file with the code above but need to clean up this list to the required output
python-3.x
fd = open(nom_fichier, 'r')
liste_chaine = fd.readlines()
liste_chaine2 =
for item in liste_chaine:
if item not in "'noirn','blancn','Humainn', 'Ordinateurn', 'Falsen', 'Truen":
liste_chaine2.append(item)
liste_chaine2 = [i.replace('n', '') for i in liste_chaine2]
return liste_chaine2
['3,3,blanc', '3,4,noir', '4,3,noir', '4,4,blanc']
i am reading a file and trying to return a string output exactly like :
3,3,blanc
4,3,noir
3,4,white
i cleaned the file with the code above but need to clean up this list to the required output
python-3.x
python-3.x
edited Nov 20 at 23:32
asked Nov 20 at 22:10
RIchard Robitaille
154
154
sorry its returning
– RIchard Robitaille
Nov 20 at 22:11
you probably used"".join
instead of",".join
. Show your code
– Jean-François Fabre
Nov 20 at 22:14
It seems you have a problem with your code. However, we can't help unless we have code or information that can reproduce the problem. Otherwise, we are just blindly guessing.
– FrankerZ
Nov 20 at 22:18
I have modified and added much more info as requested, if you need more let me know.
– RIchard Robitaille
Nov 20 at 23:44
add a comment |
sorry its returning
– RIchard Robitaille
Nov 20 at 22:11
you probably used"".join
instead of",".join
. Show your code
– Jean-François Fabre
Nov 20 at 22:14
It seems you have a problem with your code. However, we can't help unless we have code or information that can reproduce the problem. Otherwise, we are just blindly guessing.
– FrankerZ
Nov 20 at 22:18
I have modified and added much more info as requested, if you need more let me know.
– RIchard Robitaille
Nov 20 at 23:44
sorry its returning
– RIchard Robitaille
Nov 20 at 22:11
sorry its returning
– RIchard Robitaille
Nov 20 at 22:11
you probably used
"".join
instead of ",".join
. Show your code– Jean-François Fabre
Nov 20 at 22:14
you probably used
"".join
instead of ",".join
. Show your code– Jean-François Fabre
Nov 20 at 22:14
It seems you have a problem with your code. However, we can't help unless we have code or information that can reproduce the problem. Otherwise, we are just blindly guessing.
– FrankerZ
Nov 20 at 22:18
It seems you have a problem with your code. However, we can't help unless we have code or information that can reproduce the problem. Otherwise, we are just blindly guessing.
– FrankerZ
Nov 20 at 22:18
I have modified and added much more info as requested, if you need more let me know.
– RIchard Robitaille
Nov 20 at 23:44
I have modified and added much more info as requested, if you need more let me know.
– RIchard Robitaille
Nov 20 at 23:44
add a comment |
1 Answer
1
active
oldest
votes
You can split your string and put it together again to meet your requirements:
string = '33blanc 34noir 43noir 44blanche'
result = 'n'.join(['{},{},{}'.format(v[0], v[1], v[2:]) for v in string.split()])
print(result)
3,3,blanc
3,4,noir
4,3,noir
4,4,blanche
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
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%2f53402365%2fpython-add-to-string-and-return%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
You can split your string and put it together again to meet your requirements:
string = '33blanc 34noir 43noir 44blanche'
result = 'n'.join(['{},{},{}'.format(v[0], v[1], v[2:]) for v in string.split()])
print(result)
3,3,blanc
3,4,noir
4,3,noir
4,4,blanche
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
add a comment |
You can split your string and put it together again to meet your requirements:
string = '33blanc 34noir 43noir 44blanche'
result = 'n'.join(['{},{},{}'.format(v[0], v[1], v[2:]) for v in string.split()])
print(result)
3,3,blanc
3,4,noir
4,3,noir
4,4,blanche
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
add a comment |
You can split your string and put it together again to meet your requirements:
string = '33blanc 34noir 43noir 44blanche'
result = 'n'.join(['{},{},{}'.format(v[0], v[1], v[2:]) for v in string.split()])
print(result)
3,3,blanc
3,4,noir
4,3,noir
4,4,blanche
You can split your string and put it together again to meet your requirements:
string = '33blanc 34noir 43noir 44blanche'
result = 'n'.join(['{},{},{}'.format(v[0], v[1], v[2:]) for v in string.split()])
print(result)
3,3,blanc
3,4,noir
4,3,noir
4,4,blanche
answered Nov 20 at 22:22
Aurora Wang
682216
682216
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
add a comment |
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
you should not answer unclear questions like that. It's just like guessing
– Jean-François Fabre
Nov 20 at 22:25
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Actually my return is a sting on every line so that doesnt work. my bad i didnt explain correctly. The striing is 33blanc, then a new line...
– RIchard Robitaille
Nov 20 at 23:07
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
Thanks Aurora your code really helped me finish my method.
– RIchard Robitaille
Nov 21 at 0:00
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%2f53402365%2fpython-add-to-string-and-return%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
sorry its returning
– RIchard Robitaille
Nov 20 at 22:11
you probably used
"".join
instead of",".join
. Show your code– Jean-François Fabre
Nov 20 at 22:14
It seems you have a problem with your code. However, we can't help unless we have code or information that can reproduce the problem. Otherwise, we are just blindly guessing.
– FrankerZ
Nov 20 at 22:18
I have modified and added much more info as requested, if you need more let me know.
– RIchard Robitaille
Nov 20 at 23:44