2

I am trying to Write some data in a CSV file using Selenium

While running the Code

labels = browser.find_elements_by_xpath('.//a[@class="question-hyperlink"]')
text = [x.text for x in labels]
data.append(text)
print(text)
with open('output.csv', 'wb') as data_file:
    writer = csv.writer(data_file)
    writer.writerows(data)

the output am getting is

data1  data2  data3  data4  data5
data6  data7  data8  data9  data10
data11 data12 data13 data14 data15

But I want to write the data as below

data1
data2
data3
data4
.....
data15

How can I do that?
please help me.

0

2 Answers 2

1

use writer = csv.writer(data_file, delimiter='\n') delimiter

labels = browser.find_elements_by_xpath('.//a[@class="question-hyperlink"]')
text = [x.text for x in labels]
data.append(text)
print(text)
with open('output.csv', 'wb') as data_file:
    writer = csv.writer(data_file, delimiter='\n')
    writer.writerows(data)
Sign up to request clarification or add additional context in comments.

Comments

1

Change data.append(text) to data.extend(text). In such a way you will have a flat list data instead of list of lists.

In order to have a single value per row, you need to make each item in data a singleton list

 writer.writerows(map(lambda x: [x], data))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.