我用beauthulsoup抓取一个网站,页面的一半有常规数据,X#个字段每个字段有一个值,下半部分有Y#个字段,每个字段都有可变数量的值(很多时候只有一个值,其他的是任意数量的值)。在
困扰我的是如何将这些数据写入一个列数一致的csv文件(这样一来,每一列总是与一个数据点相关。。。姓名,地址,年龄-随便。我使用csv库在脚本的末尾写一个csv,我通常只调用各种列表及其值,以一致的方式填充csv。在writer.writerow([section_defendant[0], section_defendant[1], section_defendant[2], section_defendant[3], section_defendant[4], section_defendant[5], section_defendant[6], section_defendant[7], section_defendant[8], section_defendant[9], section_defendant[10], section_defendant[11], section_defendant[12], section_defendant[13], section_defendant[14], entry_tuple, section_sentence[0], section_sentence[1], section_sentence[2], section_sentence[3], section_sentence[4], bond[0], bond[1], bond[2], bond[3]])
存储条目数可变的字段中的信息的方法是在列表中使用元组,这样我的变量条目元组通常看起来像:
^{pr2}$
所以值a,e,i都是同一个字段,比如“电荷”,而b、f和j都是同一个字段“日期”,即与不同电荷有关