1

For my project I do some stuff and extract a specific line from the command line output and then insert it into a list. From there I export the list into a trainingfile for a learning algorithm to use later. The problem is when i pull the information off of the command line it pulls

"Number\n"

When i append the list to a text file it doesn't print

 Number, Number, Number

Instead it is printing

Number
Number
Number

It is applying the newline character and this is nearly useless to me. With some googling I found this link Strip all the elements of a string list explaining what I thought was exactly what i needed to fix the problem. It strips out the newline character and turns the list into what i need for later use. EXCEPT for whatever reason it isn't working.

Here is my code

def exportlist(self):  
    file = open('Trainingfile.txt','a+')
    # i pass it into a variable since i ran into some odd errors if i try 
    # to apply the map function to self.vectorlist
    my_list= self.vectorlist      
    print(my_list)               # prints [ 'number\n', 'number\n'......]
    # WHAT THE ABOVE LINK AND OTHERS SAID SHOULD WORK
    strip_list= map(str.rstrip('\n'), my_list)  
    print(strip_list)            # prints "map object at 0x00000002C...."
    self.vectorlist = strip_list # test code 
    print(self.vectorlist)       # prints same as strip_list
    file.close()

I get similar results if i use chr.split instead of strip. Printing my_list prints the improperly formatted list. printing the strip_list which is supposed to be the properly formatted list, prints what i think is a memory location " 0x0000000002C...." Everything i found online is telling me that it should not be printing this. It should be printing the properly formatted list. Does anyone know why this is happening? This happens everytime i use the map function. I have tried multiple methods to strip "\n" from my list but it always returns the same thing.

Community
  • 1
  • 1
Sam
  • 293
  • 3
  • 19

2 Answers2

3

You need to convert your result into a list:

strip_list = list(map(str.rstrip('\n'), my_list))

In Python 3 map() returns a map object. This is an iterator. You can convert to a list by using list() around it.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Just did that and I wouldnt say it crashes but it goes from printing instantly to running for 30 seconds and then giving me the error 'str' object is not callable – Sam Dec 04 '15 at 09:04
  • @Sam: Because you shouldn't use `str` as a variable name, did you use `str` as a function after you **re-define it as a variable** again? – Remi Guan Dec 04 '15 at 09:07
  • `print()` is slow. Just take all `print()` out. What error? Your question does not have any error message. – Mike Müller Dec 04 '15 at 09:07
  • @Sam Your follow-up questions address different problems and are about code you don't show here. It would be better to create a new question that shows the relevant code and the error message. My answer here should solve the problem with `map()`. – Mike Müller Dec 04 '15 at 09:18
  • computer crashed from something else so that's why it ran for 30 seconds. So ignore that problem. @KevinGuan the only time I use 'str' is when i call 'map()'. What should I use instead of str? – Sam Dec 04 '15 at 09:21
0

from Documentation...

Help on class map in module builtins:

class map(object)
 |  map(func, *iterables) --> map object
 |  
 |  Make an iterator that computes the function using arguments from
 |  each of the iterables.  Stops when the shortest iterable is exhausted.

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items

So you have wrap your iterator with a list call to get the list, like :

list(map(f, list))
Aftnix
  • 4,461
  • 6
  • 26
  • 43