对于文本文件(testfile.txt
):numpy的阵列具有读()(Python)的
# blah blah blah
Unpleasant astonished an diminution up. Noisy an their of meant. Death means up civil do an offer wound of.
//Called square an in afraid direct.
{Resolution} diminution conviction so (mr at) unpleasing simplicity no.
/*No it as breakfast up conveying earnestly
当存储一个numpy的阵列内的文本文件的内容,我无法理解差之间:
当文本文件被直接打开(无read()
)并存储在numpy的阵列中,并且
B.当文本文件被首先用打开A. 0,然后存储在numpy数组中。
下面是代码:
import numpy
# open directly with no read
a = numpy.array([str(i) for i in open(r'C:\testfile.txt', 'r')])
# open with read then store in numpy *how I want to do it*
f = open(r'C:\testfile.txt', 'r').read()
b = numpy.array([str(i) for i in f])
print("A")
print(a)
print()
print("B")
print(b)
我的问题是如何改变numpy.array([str(i) for i in f])
命令,这样所产生的numpy的数组保存文本文件的内容的方式,输出的确实(见下文)。
输出:
A
['# blah blah blah\n' '\n'
'Unpleasant astonished an diminution up. Noisy an their of meant. Death means up civil do an offer wound of. \n'
'//Called square an in afraid direct. \n' '\n' '\n'
'{Resolution} diminution conviction so (mr at) unpleasing simplicity no. \n'
'/*No it as breakfast up conveying earnestly ']
B
['#' ' ' 'b' 'l' 'a' 'h' ' ' 'b' 'l' 'a' 'h' ' ' 'b' 'l' 'a' 'h' '\n' '\n'
'U' 'n' 'p' 'l' 'e' 'a' 's' 'a' 'n' 't' ' ' 'a' 's' 't' 'o' 'n' 'i' 's'
'h' 'e' 'd' ' ' 'a' 'n' ' ' 'd' 'i' 'm' 'i' 'n' 'u' 't' 'i' 'o' 'n' ' '
'u' 'p' '.' ' ' 'N' 'o' 'i' 's' 'y' ' ' 'a' 'n' ' ' 't' 'h' 'e' 'i' 'r'
' ' 'o' 'f' ' ' 'm' 'e' 'a' 'n' 't' '.' ' ' 'D' 'e' 'a' 't' 'h' ' ' 'm'
'e' 'a' 'n' 's' ' ' 'u' 'p' ' ' 'c' 'i' 'v' 'i' 'l' ' ' 'd' 'o' ' ' 'a'
'n' ' ' 'o' 'f' 'f' 'e' 'r' ' ' 'w' 'o' 'u' 'n' 'd' ' ' 'o' 'f' '.' ' '
'\n' '/' '/' 'C' 'a' 'l' 'l' 'e' 'd' ' ' 's' 'q' 'u' 'a' 'r' 'e' ' ' 'a'
'n' ' ' 'i' 'n' ' ' 'a' 'f' 'r' 'a' 'i' 'd' ' ' 'd' 'i' 'r' 'e' 'c' 't'
'.' ' ' '\n' '\n' '\n' '{' 'R' 'e' 's' 'o' 'l' 'u' 't' 'i' 'o' 'n' '}' ' '
'd' 'i' 'm' 'i' 'n' 'u' 't' 'i' 'o' 'n' ' ' 'c' 'o' 'n' 'v' 'i' 'c' 't'
'i' 'o' 'n' ' ' 's' 'o' ' ' '(' 'm' 'r' ' ' 'a' 't' ')' ' ' 'u' 'n' 'p'
'l' 'e' 'a' 's' 'i' 'n' 'g' ' ' 's' 'i' 'm' 'p' 'l' 'i' 'c' 'i' 't' 'y'
' ' 'n' 'o' '.' ' ' '\n' '/' '*' 'N' 'o' ' ' 'i' 't' ' ' 'a' 's' ' ' 'b'
'r' 'e' 'a' 'k' 'f' 'a' 's' 't' ' ' 'u' 'p' ' ' 'c' 'o' 'n' 'v' 'e' 'y'
'i' 'n' 'g' ' ' 'e' 'a' 'r' 'n' 'e' 's' 't' 'l' 'y' ' ']
对于文本文件'readlines()'通常比'read()'更有用。结果类似于在打开的文件上迭代。 – hpaulj
同意。 readlines()与在文件对象中执行“for ... in”基本相同。但问题是,直接询问如何用“read()”来完成。 –