簡體   English   中英

如何格式化列表數據並寫入 selenium python 中的 csv 文件?

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

我從網站獲取數據並將它們存儲在變量列表中。 現在我需要將這些數據發送到 CSV 文件。 網站數據打印如下所示。

從網站獲取的數據

['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']

我正在使用 for 循環使用以下代碼獲取這些數據:

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

現在我擁有 5 個列表變量中的所有數據。 如何將這些數據寫入如下格式的 CSV 文件? 在此處輸入圖像描述

您似乎想要兩個標題行。 但恐怕您的 CSV 解釋器(似乎是 MS Excel)將無法像您在屏幕截圖中顯示的那樣合並單元格。

根據您的數據結構(鍵和值混合的五個列表)看起來您可能必須半手動構造兩個標題。 這是代碼:

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')

如果我使用 MS Excel 導入該文件並在文本導入向導中將“逗號”設置為分隔符,您將得到類似的內容:

導入后的 MS Excel 屏幕截圖

您可以將其包裝到一個輔助類中,該類采用這五個列表並將write_csv()方法公開給外界。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM