'n' and 'None' appearing at end of console outputted List. How to remove them?
up vote
-1
down vote
favorite
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 |
up vote
-1
down vote
favorite
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
functionshould return thewordList, not print it. Regarding thenthere 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 |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
555k886611013
555k886611013
asked Nov 11 at 2:56
CosmicCat
505
505
functionshould return thewordList, not print it. Regarding thenthere 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 |
functionshould return thewordList, not print it. Regarding thenthere 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
up vote
0
down vote
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
Did this solve your problems?
– user8408080
Nov 22 at 11:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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
Did this solve your problems?
– user8408080
Nov 22 at 11:53
add a comment |
up vote
0
down vote
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
Did this solve your problems?
– user8408080
Nov 22 at 11:53
add a comment |
up vote
0
down vote
up vote
0
down vote
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
87738
87738
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
Did this solve your problems?
– user8408080
Nov 22 at 11:53
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
Did this solve your problems?
– user8408080
Nov 22 at 11:53
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
Did this solve your problems?
– user8408080
Nov 22 at 11:53
Did this solve your problems?
– user8408080
Nov 22 at 11:53
add a comment |
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
functionshould return thewordList, not print it. Regarding thenthere 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