Chand Prakash
Chand Prakash

Reputation: 1

write output in CSV file

I am having a code to fetch the data and store the output into a csv file using python.

sample output.

66.100:5
247.600:388

when this is being written in csv file it like below.

6
6
.
1
0
0
:
5
2
4
7
.
6
0
0
:
3
8
8

I want the output as 66.100 in one column and 5 in second column

row 1 -66.100 5
row 2- 247.600 388

Please help!!

    with open('lat_lon.csv', 'a') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerows(last_price[0].text + ':' + Stock[0].text)

Upvotes: 0

Views: 150

Answers (1)

rdas
rdas

Reputation: 21285

writer.writerows(last_price[0].text + ':' + Stock[0].text)

Here, the writer is thinking that the string you've given it is the iterable that it needs to convert into rows. So it's trying to do something like:

for x in last_price[0].text + ':' + Stock[0].text:
   writerow(x)

Now, iterating over the string gives it each character in the string - hence the output.

What you need to do:

  1. Use : as the delimiter for your CSV file.

  2. Put each of the rows in a list and pass that list to writerows or use writerow.

Upvotes: 4

Related Questions