2

こんにちは、私はかなり単純なことをしようとしています

私はArray、slideDown 通知 div に大量の文がランダムに使用されています。PyCharm で非常に長い 1 行を使用したくないので、txt ファイルから .txt ファイルに文をインポートするだけでよいと考えましたArray

thisthisを見つけたので、 numpyをインポートしましたが、以下のコードを使用すると、行で壊れます(エラーメッセージにスキップします) success_msgs。エラーも発生しません。

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."]

何かご意見は?:(


My Text File (py ファイルと同じフォルダーにあります。

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._

ここに画像の説明を入力

デバッガ
ここに画像の説明を入力

私の定義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 デバッグ:

ここに画像の説明を入力

4

1 に答える 1

1

まず、文字列 dtype を使用するように指示しますdtype='S72'(または予想される最大文字数)。

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')

または、すべての行がアンダースコアで終わり、アンダースコアを含めたくない場合は、最初の列のみを取得するようにdelimiter='_'設定できます。usecols=0

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')

しかし、numpy を使用せずにファイルをロードできない理由はありません。

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.']
于 2013-09-13T20:58:33.067 に答える