python - 如何将文本文件导入 Python 中的数组( Pyramid )

标签 python arrays numpy text-files pyramid

你好,我正在尝试做一些非常简单的事情

我有一个 Array,其中有大量句子随机用于 slideDown 通知 div。因为我不想在 PyCharm 中有很长的 1 行,所以我想我可以将句子从 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."]

有什么想法吗? :(


我的文本文件(与 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._

enter image description here

调试器
enter image description here

我的 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 调试:

enter image description here

最佳答案

首先,告诉它使用带有 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.']

关于python - 如何将文本文件导入 Python 中的数组( Pyramid ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795089/

相关文章:

python - 循环打印重复天数

python - 生成缺失时间间隔的列表

JQUERY:获取已选中和未选中复选框的ID

python - 查找回归平面并将其绘制到一组点

pandas - 生成具有条件的随机数列表 - numpy

python - 列表附加在循环问题中

python - 在 wx.calendar CalendarCtrl 小部件上设置日期

java 。查找数组列表中出现次数最多的字符串长度

c - 如何从数组中取出字符并将它们存储在变量中?

python - 在Tensorflow中批量读取numpy矩阵