'n' and 'None' appearing at end of console outputted List. How to remove them?












-1














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?










share|improve this question
























  • 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










  • 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
















-1














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?










share|improve this question
























  • 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










  • 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














-1












-1








-1







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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:34









Jonathan Leffler

559k896651016




559k896651016










asked Nov 11 at 2:56









CosmicCat

825




825












  • 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










  • 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












  • 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












1 Answer
1






active

oldest

votes


















0














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(..)





share|improve this answer























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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(..)





share|improve this answer























  • 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
















0














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(..)





share|improve this answer























  • 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














0












0








0






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(..)





share|improve this answer














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(..)






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud