如何在Python

时间:2016-06-22 21:32:08

标签: python arrays csv numpy

我正在尝试将数组导出到txt或csv文件。我一直在尝试numpy,但我总是得到一些错误 TypeError: Mismatch between array dtype ('<U14') and format specifier ('%.18e')

这是我的代码没有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)

真的需要一些帮助。

2 个答案:

答案 0 :(得分:1)

看起来你正在做类似的事情:

 <ul class="inline-list">
     <li class="col-xs-6"......

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

由于我没有指定In [1342]: np.savetxt('test.txt',peoples) ... TypeError: Mismatch between array dtype ('<U14') and format specifier ('%.18e %.18e') ,因此构建了一个默认值,由两个fmt字段组成。这对数字的一般格式很有用。但数据包括14个字符串('U14' - Python3中的unicode)。

如果我告诉它使用%.18e,通用字符串格式,我得到:

%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 会更好。

我掩饰了另一个细微差别。 fmt='%20s'是列表清单。 peoples适用于数组,因此它首先将其转换为数组:

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') 个字符串。所以我必须用U14格式化两列。我不能在第一个使用数字格式。我首先要做的是创建一个带有数字字段和字符串字段的结构化数组。我知道该怎么做,但我现在不会详细介绍。

根据评论,将每个%s行格式化为完整字符串并将其写入文件可能更简单。

peoples

答案 1 :(得分:1)

hpauj's回答解释了为什么您的代码出错但使用 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")])