1

Hi so I'm trying to do something pretty simple

I have an Array with a ton of sentences being used randomly for a slideDown notification div. Since I don't want to have a really long 1 line in PyCharm I figured I could just import the sentences from a txt file into my Array.

I found this and this so I imported numpy, and using the code below, however, it breaks(and skips to my error messages) on the success_msgs line, I don't get an error too.

def create_request(self):
    # text_file = open("success_requests.txt", "r")
    # lines = text_file.readlines()
    # print lines

    success_msgs = loadtxt("success_request.txt", comments="#", delimiter="_", unpack=False)
    #success_msgs = ['The intro request was sent successfully', "Request sent off, let's see what happens!", "Request Sent. Good luck, may the force be with you.", "Request sent, that was great! Like Bacon."]

Any thoughts? :(


My Text File (which is in the same folder as the py file:

The intro request was sent successfully_
Request sent off, let's see what happens!_
Request Sent. Good luck, may the force be with you._
Request sent, that was great! Like Bacon._

enter image description here

Debugger
enter image description here

my def genfromtxt

def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
           skiprows=0, skip_header=0, skip_footer=0, converters=None,
           missing='', missing_values=None, filling_values=None,
           usecols=None, names=None,
           excludelist=None, deletechars=None, replace_space='_',
           autostrip=False, case_sensitive=True, defaultfmt="f%i",
           unpack=None, usemask=False, loose=True, invalid_raise=True):

genfromtxt debug:

enter image description here

Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
  • Using Numpy for this task **does not make any sense**. Fundamentally Numpy is intended for manipulating *numerical* data, and `loadtxt` is intended for loading *two-dimensional* array data from a file, such as represented by a CSV file. – Karl Knechtel Aug 29 '23 at 19:56

1 Answers1

1

Firstly, tell it to use a string dtype with dtype='S72' (or whatever maximum number of characters you expect).

In [368]: np.genfromtxt("success_request.txt", delimiter='\n', dtype='S72')
Out[368]: 
array(['The intro request was sent successfully_',
       "Request sent off, let's see what happens!_",
       'Request Sent. Good luck, may the force be with you._',
       'Request sent, that was great! Like Bacon._'], 
      dtype='|S72')

Or, if every line ends with an underscore, and you do not want to include the underscores, you could set delimiter='_' and usecols=0 to get only the first column:

In [372]: np.genfromtxt("success_request.txt", delimiter='_', usecols=0, dtype='S72')
Out[372]: 
array(['The intro request was sent successfully',
       "Request sent off, let's see what happens!",
       'Request Sent. Good luck, may the force be with you.',
       'Request sent, that was great! Like Bacon.'], 
      dtype='|S72')

But there's no reason why you can't just load the file without using numpy with

In [369]: s = open("success_request.txt",'r')

In [379]: [line.strip().strip('_') for line in s.readlines()]
Out[379]: 
['The intro request was sent successfully',
 "Request sent off, let's see what happens!",
 'Request Sent. Good luck, may the force be with you.',
 'Request sent, that was great! Like Bacon.']
askewchan
  • 45,161
  • 17
  • 118
  • 134
  • I'm trying these, but still not working :( `s = [np.genfromtxt("success_request.txt", delimiter='\n', dtype='S100')] success_msgs = [s]` Having trouble with the open method too. – Leon Gaban Sep 13 '13 at 21:12
  • @Leon You don't need either of those square brackets. just `success_msgs = np.genfromtxt("success_request.txt", delimiter='_', usecols=0, dtype='S72')` should be sufficient. `genfromtxt` returns an array, so you don't need to make a list out of it with brackets. – askewchan Sep 13 '13 at 21:14
  • 1
    @SaulloCastro That also works for me, I end up with `dtype='S51'`. – askewchan Sep 13 '13 at 21:26
  • Hmm I'm still trying, thanks for your patience, does the added `genformtxt` def and the output help? I tried `dtype=None` also. I'm starting to think perhaps it's something else in this project. Another dev suggested I use a string like `message = """ blah, blah """` – Leon Gaban Sep 13 '13 at 21:52
  • 1
    @Leon I don't understand why you have a `def genfromtxt` line... you should just import it from numpy: `from numpy import genfromtxt` – askewchan Sep 13 '13 at 22:16