'n' and 'None' appearing at end of console outputted List. How to remove them?
I'm new to files in Python and I'm having trouble removing the 'n' and the word None in my output console. Here's my code:
def function(inputFile, wordFile):
input = open(inputFile, 'r')
words = open(wordFile, 'r')
wordList =
for line in words:
wordList.append(line.split(','))
print(wordList)
words.close()
##call function
result = function("file1.txt","file2.txt")
print(result)
print()
my file2.txt/wordFile/words looks like this:
hello,world
123,456
This is the output I get:
['hello', 'worldn']
['123', '456n']
None
I know there's a lot going on but how do I remove the 'n' and None?
python list function file
add a comment |
I'm new to files in Python and I'm having trouble removing the 'n' and the word None in my output console. Here's my code:
def function(inputFile, wordFile):
input = open(inputFile, 'r')
words = open(wordFile, 'r')
wordList =
for line in words:
wordList.append(line.split(','))
print(wordList)
words.close()
##call function
result = function("file1.txt","file2.txt")
print(result)
print()
my file2.txt/wordFile/words looks like this:
hello,world
123,456
This is the output I get:
['hello', 'worldn']
['123', '456n']
None
I know there's a lot going on but how do I remove the 'n' and None?
python list function file
function
should return thewordList
, not print it. Regarding then
there is e.g.str.strip
– Michael Butscher
Nov 11 at 3:00
return did remove the 'None' part, thank you! But doesn't str.strip not work on Lists?
– CosmicCat
Nov 11 at 3:02
You must apply it to the string before it is splitted.
– Michael Butscher
Nov 11 at 3:03
A good way to write your code would be:with open(wordFile) as words: return [line.rstrip().split(',') for line in words]
. Thus, the whole function will be a one-liner.
– DYZ
Nov 11 at 3:11
add a comment |
I'm new to files in Python and I'm having trouble removing the 'n' and the word None in my output console. Here's my code:
def function(inputFile, wordFile):
input = open(inputFile, 'r')
words = open(wordFile, 'r')
wordList =
for line in words:
wordList.append(line.split(','))
print(wordList)
words.close()
##call function
result = function("file1.txt","file2.txt")
print(result)
print()
my file2.txt/wordFile/words looks like this:
hello,world
123,456
This is the output I get:
['hello', 'worldn']
['123', '456n']
None
I know there's a lot going on but how do I remove the 'n' and None?
python list function file
I'm new to files in Python and I'm having trouble removing the 'n' and the word None in my output console. Here's my code:
def function(inputFile, wordFile):
input = open(inputFile, 'r')
words = open(wordFile, 'r')
wordList =
for line in words:
wordList.append(line.split(','))
print(wordList)
words.close()
##call function
result = function("file1.txt","file2.txt")
print(result)
print()
my file2.txt/wordFile/words looks like this:
hello,world
123,456
This is the output I get:
['hello', 'worldn']
['123', '456n']
None
I know there's a lot going on but how do I remove the 'n' and None?
python list function file
python list function file
edited Nov 11 at 3:34
Jonathan Leffler
559k896651016
559k896651016
asked Nov 11 at 2:56
CosmicCat
825
825
function
should return thewordList
, not print it. Regarding then
there is e.g.str.strip
– Michael Butscher
Nov 11 at 3:00
return did remove the 'None' part, thank you! But doesn't str.strip not work on Lists?
– CosmicCat
Nov 11 at 3:02
You must apply it to the string before it is splitted.
– Michael Butscher
Nov 11 at 3:03
A good way to write your code would be:with open(wordFile) as words: return [line.rstrip().split(',') for line in words]
. Thus, the whole function will be a one-liner.
– DYZ
Nov 11 at 3:11
add a comment |
function
should return thewordList
, not print it. Regarding then
there is e.g.str.strip
– Michael Butscher
Nov 11 at 3:00
return did remove the 'None' part, thank you! But doesn't str.strip not work on Lists?
– CosmicCat
Nov 11 at 3:02
You must apply it to the string before it is splitted.
– Michael Butscher
Nov 11 at 3:03
A good way to write your code would be:with open(wordFile) as words: return [line.rstrip().split(',') for line in words]
. Thus, the whole function will be a one-liner.
– DYZ
Nov 11 at 3:11
function
should return the wordList
, not print it. Regarding the n
there is e.g. str.strip
– Michael Butscher
Nov 11 at 3:00
function
should return the wordList
, not print it. Regarding the n
there is e.g. str.strip
– Michael Butscher
Nov 11 at 3:00
return did remove the 'None' part, thank you! But doesn't str.strip not work on Lists?
– CosmicCat
Nov 11 at 3:02
return did remove the 'None' part, thank you! But doesn't str.strip not work on Lists?
– CosmicCat
Nov 11 at 3:02
You must apply it to the string before it is splitted.
– Michael Butscher
Nov 11 at 3:03
You must apply it to the string before it is splitted.
– Michael Butscher
Nov 11 at 3:03
A good way to write your code would be:
with open(wordFile) as words: return [line.rstrip().split(',') for line in words]
. Thus, the whole function will be a one-liner.– DYZ
Nov 11 at 3:11
A good way to write your code would be:
with open(wordFile) as words: return [line.rstrip().split(',') for line in words]
. Thus, the whole function will be a one-liner.– DYZ
Nov 11 at 3:11
add a comment |
1 Answer
1
active
oldest
votes
To get rid of whitespace characters you can use strip
:
wordList.append(line.strip().split(','))
Also your function does not not return anything, so result = function("file1.txt","file2.txt")
will assign nothing to result
also called None
in python. To have it return something, use return
at the end of the function:
return wordlist
It is also possible to return mulitple variables:
return var1, var2, ...
You can get them by
a, b, ... = function(..)
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5: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%2f53245457%2fn-and-none-appearing-at-end-of-console-outputted-list-how-to-remove-them%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
To get rid of whitespace characters you can use strip
:
wordList.append(line.strip().split(','))
Also your function does not not return anything, so result = function("file1.txt","file2.txt")
will assign nothing to result
also called None
in python. To have it return something, use return
at the end of the function:
return wordlist
It is also possible to return mulitple variables:
return var1, var2, ...
You can get them by
a, b, ... = function(..)
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5:03
add a comment |
To get rid of whitespace characters you can use strip
:
wordList.append(line.strip().split(','))
Also your function does not not return anything, so result = function("file1.txt","file2.txt")
will assign nothing to result
also called None
in python. To have it return something, use return
at the end of the function:
return wordlist
It is also possible to return mulitple variables:
return var1, var2, ...
You can get them by
a, b, ... = function(..)
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5:03
add a comment |
To get rid of whitespace characters you can use strip
:
wordList.append(line.strip().split(','))
Also your function does not not return anything, so result = function("file1.txt","file2.txt")
will assign nothing to result
also called None
in python. To have it return something, use return
at the end of the function:
return wordlist
It is also possible to return mulitple variables:
return var1, var2, ...
You can get them by
a, b, ... = function(..)
To get rid of whitespace characters you can use strip
:
wordList.append(line.strip().split(','))
Also your function does not not return anything, so result = function("file1.txt","file2.txt")
will assign nothing to result
also called None
in python. To have it return something, use return
at the end of the function:
return wordlist
It is also possible to return mulitple variables:
return var1, var2, ...
You can get them by
a, b, ... = function(..)
edited Nov 11 at 9:31
answered Nov 11 at 3:03
user8408080
1,121139
1,121139
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5:03
add a comment |
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5:03
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5:03
Just needed something else answered. The problem I just realized with this solution is that once I 'return wordlist', I can't return anything else for the remainder of the 'function'. Is there any way we can make this work without using return or by using two functions?
– CosmicCat
Nov 11 at 5: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%2f53245457%2fn-and-none-appearing-at-end-of-console-outputted-list-how-to-remove-them%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
function
should return thewordList
, not print it. Regarding then
there is e.g.str.strip
– Michael Butscher
Nov 11 at 3:00
return did remove the 'None' part, thank you! But doesn't str.strip not work on Lists?
– CosmicCat
Nov 11 at 3:02
You must apply it to the string before it is splitted.
– Michael Butscher
Nov 11 at 3:03
A good way to write your code would be:
with open(wordFile) as words: return [line.rstrip().split(',') for line in words]
. Thus, the whole function will be a one-liner.– DYZ
Nov 11 at 3:11