0

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)
1
  • 2
    It'll be tough to help without knowing what last_price and Stock are. Commented May 16, 2019 at 15:33

1 Answer 1

4
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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for suggestion!! I tried to store the variable into list but did not work. I am having a loop so i will receive five row. how the variable(last_price & Stock value can be stored in list.
We'll need to know what data you're trying to save and the expected output
this is the data coming as output if i use to print print (Stock[0].text + ' | ' + last_price[0].text) I want the same data is stored in csv file with the same format. stock in one column and price in second column Output:- stock | Price (5) | 66.700 (388) | 248.000 (2888) | 68.600 (338) | 3.500 (1716) | 1.080
It's hard to see what's going on there. Can you print the variables separately.
I tried to print the one variable but still the csv file is having each element in a new row. This is the code
|

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.