如何在Python中将文本文件导入数组(金字塔)
嗨,我想做一些简单的事情 我有一个如何在Python中将文本文件导入数组(金字塔),python,arrays,numpy,text-files,pyramid,Python,Arrays,Numpy,Text Files,Pyramid,嗨,我想做一些简单的事情 我有一个数组,其中有大量的句子被随机用于一个向下滑动的通知div。由于我不想在PyCharm中有一个很长的1行,我想我可以将这些句子从一个txt文件导入我的数组 我找到并导入了,使用下面的代码,但是,它在成功\u msgs行中断(并跳过我的错误消息),我也没有收到错误 def create_request(self): # text_file = open("success_requests.txt", "r") # lines = text_file.
数组
,其中有大量的句子被随机用于一个向下滑动的通知div。由于我不想在PyCharm中有一个很长的1行,我想我可以将这些句子从一个txt文件导入我的数组
我找到并导入了,使用下面的代码,但是,它在成功\u 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._
调试器我的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调试:
首先,告诉它使用带有
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.']
我正在尝试这些,但仍然不起作用:(
s=[np.genfromtxt(“success\u request.txt”,delimiter='\n',dtype='S100')]success\u msgs=[s]
open方法也有问题。@Leon您不需要这些方括号中的任何一个。只要success\u msgs=np.genfromtxt(“success\u request.txt”,delimiter='',usecols=0,dtype='S72'))
应该足够了。genfromtxt
返回一个数组,所以你不需要用括号列出它。@saulocastro这对我也适用,我最终得到的是dtype='S51'
。谢谢你的耐心,我还在尝试,添加的genformtxt
def和输出有帮助吗?我尝试了dtype=None
>另外。我开始认为可能是这个项目中的其他东西。另一个开发人员建议我使用类似message=“”blah,blah”“”
@Leon我不明白为什么你有一行def genfromtxt
行…你应该从numpy导入它:从numpy导入genfromtxt
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.']