0

I want to write my data crawl to .csv file, but it have more '\n'. This is my code:

for data in soup.find_all('b', {'class' : 'tur highlight'}):
        write.writerow([word, data.get_text()])

And this is the result:

enter image description here

How to remove the blank rows? I use python 3.5

dev-x
  • 897
  • 3
  • 15
  • 30

2 Answers2

2

you can try this

for data in soup.find_all('b', {'class' : 'tur highlight'}):
        if word == '\n' and data.get_text() == '\n':
           pass
        else:
           write.writerow([word, data.get_text()])

or you can use

with open('test_csv.csv','w',newline='') as file:

for python 3

and

with open('test_csv.csv','wb') as file:

for python2

Exprator
  • 26,992
  • 6
  • 47
  • 59
2

this problems occurs when using python on windows:

if you're using Python 3.x, add this parameter when opening the file to write to:

newline=''

example:

with open('test_csv.csv','w',newline='') as file:

if you're using Python 2, open the file using "wb" instead of "w".

with open('test_csv.csv','wb') as file:

reference : here

Hope this was helpful.

Rayhane Mama
  • 2,374
  • 11
  • 20