简体   繁体   English

如何格式化列表数据并写入 selenium python 中的 csv 文件?

[英]How to format list data and write to csv file in selenium python?

I'm getting data from a website and storing them inside a list of variables.我从网站获取数据并将它们存储在变量列表中。 Now I need to send these data to a CSV file.现在我需要将这些数据发送到 CSV 文件。 The website data is printed and shown below.网站数据打印如下所示。

The data getting from the Website从网站获取的数据

['Company Name: PATRY PLC', 'Contact Name: Jony Deff', 'Company ID: 234567', 'CS ID: 236789', 'MI/MC:', 'Road Code:']
['Mailing Address:', 'Street: 19700 I-45 Spring, TX 77373', 'City: SPRING', 'State: TX', 'Postal Code: 77388', 'Country: US']
['Physical Address:', 'Street: 1500-1798 Runyan Ave Houston, TX 77039, USA', 'City: HOUSTON', 'State: TX', 'Postal Code: 77039', 'Country: US']
['Registration Period', 'Registration Date/Time', 'Registration ID', 'Status']
['2020-2025', 'MAY-10-2020  15:54:12', '26787856889l', 'Active']

I'm using for loop to get these data using the below code:我正在使用 for 循环使用以下代码获取这些数据:

listdata6 = []
for c6 in cells6:
    listdata6.append(c6.text)

Now I have all data inside the 5 list variables.现在我拥有 5 个列表变量中的所有数据。 How can I write these data into CSV file like the below format?如何将这些数据写入如下格式的 CSV 文件? 在此处输入图像描述

You seem to want to have two header rows.您似乎想要两个标题行。 But I'm afraid your CSV interpreter (which seem to be MS Excel) won't be able to merge cells like you show on the screenshot.但恐怕您的 CSV 解释器(似乎是 MS Excel)将无法像您在屏幕截图中显示的那样合并单元格。

Based on the structure of your data (five lists where keys and values are mixed) looks like you probably have to construct both headers semi-manually.根据您的数据结构(键和值混合的五个列表)看起来您可能必须半手动构造两个标题。 Here is the code:这是代码:

company_info = ['Company Name: PATRY PLC', 'Contact Name: Jony Deff', 'Company ID: 234567', 'CS ID: 236789', 'MI/MC:', 'Road Code:']
mailaddr_info = ['Mailing Address:', 'Street: 19700 I-45 Spring, TX 77373', 'City: SPRING', 'State: TX', 'Postal Code: 77388', 'Country: US']
physaddr_info = ['Physical Address:', 'Street: 1500-1798 Runyan Ave Houston, TX 77039, USA', 'City: HOUSTON', 'State: TX', 'Postal Code: 77039', 'Country: US']
reg_data = ['Registration Period', 'Registration Date/Time', 'Registration ID', 'Status']
status_data = ['2020-2025', 'MAY-10-2020  15:54:12', '26787856889l', 'Active']

# composing 1st header's row
header1 = ''.join(',' for i in range(len(company_info))) # add commas
header1 += mailaddr_info[0].strip(':') # adds 1st item which is header of that data
header1 += ''.join(',' for i in range(1, len(mailaddr_info)))
header1 += physaddr_info[0].strip(':') # adds 1st item which is header of that data
header1 += ''.join(',' for i in range(1, len(physaddr_info)))
header1 += ''.join(',' for i in range(len(reg_data))) # add commas

# composing 2nd header's row
header2 = ','.join( item.split(':')[0].strip(' ') for item in company_info) + ','
header2 += ','.join( item.split(':')[0].strip(' ') for item in mailaddr_info[1:]) + ','
header2 += ','.join( item.split(':')[0].strip(' ') for item in physaddr_info[1:]) + ','
header2 += ','.join( item.split(':')[0].strip(' ') for item in reg_data)

# finally, the data row. Note we replace comma with empty char because some items contain comma.
# You can further elaborate by encapsulating comma-containing items with quotes "" which
# is treated as text by CSV interpreters.
data_row = ','.join( item.split(':')[-1].strip(' ') for item in company_info)
data_row += ','.join( item.split(':')[-1].strip(' ').replace(',','') for item in mailaddr_info)
data_row += ','.join( item.split(':')[-1].strip(' ').replace(',','') for item in physaddr_info)+ ','
data_row += ','.join( item for item in status_data)

# writing the data to CSV file
with open("test_file.csv", "w") as f:
    f.write(header1 + '\n')
    f.write(header2 + '\n')
    f.write(data_row + '\n')

If I import that file using MS Excel and set 'Comma' as separator in text import wizard you will get something like that:如果我使用 MS Excel 导入该文件并在文本导入向导中将“逗号”设置为分隔符,您将得到类似的内容:

导入后的 MS Excel 屏幕截图

You can wrap it into a helper class which takes these five lists and exposes write_csv() method to the outside world.您可以将其包装到一个辅助类中,该类采用这五个列表并将write_csv()方法公开给外界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM