[英]How to export array in csv or txt in Python
I'm trying to export array to txt or csv file. 我正在尝试将数组导出到txt或csv文件。 I've been trying with numpy but i always get some error like
TypeError: Mismatch between array dtype ('<U14') and format specifier ('%.18e')
我一直在尝试使用numpy,但是我总是会遇到类似
TypeError: Mismatch between array dtype ('<U14') and format specifier ('%.18e')
错误TypeError: Mismatch between array dtype ('<U14') and format specifier ('%.18e')
Here is my code without numpy that works great but I need help with part how to export it. 这是我没有numpy的代码,效果很好,但是我需要部分如何导出它的帮助。
peoples = []
for content in driver.find_elements_by_class_name('x234'):
people = content.find_element_by_xpath('.//div[@class="zstrim"]').text
if people != "Django" and people != "Rooky" :
pass
peoples.append([people, 1, datetime.now().strftime("%d/%m/%y %H:%M")])
print(peoples)
Really need some help with this. 确实需要一些帮助。
Looks like you are doing something like: 看起来您正在执行以下操作:
In [1339]: peoples=[]
In [1340]: for _ in range(3):
......: peoples.append([234, datetime.datetime.now().strftime("%d/%m/%y %H:%M")])
......:
In [1341]: peoples
Out[1341]: [[234, '22/06/16 14:57'], [234, '22/06/16 14:57'], [234, '22/06/16 14:57']]
peoples
is an array (or here a list of lists), that contains, among other things formatted dates. peoples
是一个数组(或此处的列表列表),其中包含格式化日期等。
In [1342]: np.savetxt('test.txt',peoples)
...
TypeError: Mismatch between array dtype ('<U14') and format specifier ('%.18e %.18e')
Since I didn't specify fmt
it constructed a default one, consisting of two %.18e
fields. 由于我没有指定
fmt
因此它构造了一个默认值,由两个%.18e
字段组成。 That's great for general formatting of numbers. 这对于数字的一般格式非常有用。 But the data includes 14 characters strings ('U14' - unicode in Python3).
但是数据包含14个字符串('U14'-Python3中的unicode)。
If I tell it to use %s
, the generic string format, I get: 如果我告诉它使用
%s
(通用字符串格式),则会得到:
In [1346]: np.savetxt('test.txt',peoples, fmt='%s', delimiter=',')
In [1347]: cat test.txt
234,22/06/16 14:57
234,22/06/16 14:57
234,22/06/16 14:57
Not ideal, but still it works. 不理想,但仍然有效。
fmt='%20s'
would be better. fmt='%20s'
会更好。
I glossed over a another nuance. 我掩饰了另一个细微差别。
peoples
is a list of lists. peoples
是清单清单。 np.savetxt
works with arrays, so it first turns that into an array with: np.savetxt
可用于数组,因此首先将其转换为具有以下内容的数组:
In [1360]: np.array(peoples)
Out[1360]:
array([['234', '22/06/16 14:57'],
['234', '22/06/16 14:57'],
['234', '22/06/16 14:57']],
dtype='<U14')
But this turns both columns into U14
strings. 但这会将两列都转换为
U14
字符串。 So I have to format both columns with %s
. 所以我必须用
%s
格式化两列。 I can't use a numeric format on the first. 我不能先使用数字格式。 What I need to do first is make a structured array with a numeric field(s) and a string field.
我首先需要做的是一个带有数字字段和字符串字段的结构化数组。 I know how to do that, but I won't get into the details now.
我知道该怎么做,但是我现在不详细介绍。
As per comments, it could be simpler to format each peoples
line as a complete string, and write that to a file. 根据注释,将每个
peoples
行格式化为完整的字符串并将其写入文件可能更简单。
In [1378]: with open('test.txt','w') as f:
for _ in range(3):
f.write('%10d,%20s\n'%(234, datetime.datetime.now().strftime("%d/%m/%y %H:%M")))
......:
In [1379]: cat test.txt
234, 22/06/16 15:18
234, 22/06/16 15:18
234, 22/06/16 15:18
hpauj's answer explains you why your code error but using the csv lib and writing as you go is probably a lot easier: hpauj的答案向您说明了为什么您的代码出错,但是使用csv lib并随手编写可能会容易得多 :
import csv
with open("out.csv", "w") as f:
wr = csv.writer(f)
for content in driver.find_elements_by_class_name('x234'):
people = content.find_element_by_xpath('.//div[@class="zstrim"]').text
if people != "Django" and people != "Rooky":
wr.writerow([people, 1, datetime.now().strftime("%d/%m/%y %H:%M")])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.