2

I have designed code as:

import csv
import numpy as np
data = [['Diameter', 'color', 'no']]
with open('samp1.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')
    for row in data:
        writer.writerow(row)
    for i in np.arange(20,30,0.2):
        writer.writerow(i)

f.close()

And I want to save numbers from 20-30 with an increment of 0.2 in the diameter column, but it's giving errors and not even saving the CSV file. Can someone suggest any solution? Even there are various ranges for other columns too, so I need the same method to work for that code.

CSV example

diameter    color     number
20          2         3
20          2.5       3
20          3         3
20          3.5       3
20.2        2         3
20.2        2.5       3
20.2        3         3
20.2        3.5       3
.
.
.
.
22          2         4
22          2.5       4
22          3         4
22          3.5       4
22.2        2         4
22.2        2.5       4
22.2        3         4
22.2        3.5       4
4
  • "its giving errors" => provide full error & traceback Commented Aug 8, 2018 at 9:06
  • its just sample code from my program where I have lots of columns and for each column I need to save different kind of data like in first column numbers from 20-30 but with difference of .2 and in another column 2-5 with difference .5 each particular number needed to be matched with new one.. Commented Aug 8, 2018 at 9:13
  • can you add a sample o/p you are expecting ? Commented Aug 8, 2018 at 9:21
  • @Vikas Damodar I have added sample o/p so can you guide me please. Thankyou Commented Aug 8, 2018 at 9:37

2 Answers 2

1

I guess this can help you:

import csv

data = ['Diameter', 'color', 'no']
numb = [i for i in range(20,30,2)]
print(numb)
with open('samp1.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(data)
    for i in numb:
            writer.writerow([i,i,i])

f.close()
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou @RamG its working for me.And can I know if we want to save this range data in particular column is it possible?
Happy to hear problem solved , please mark this as answer , yup u can do it , declare new array for them and put them instead of "i" in for i in numb: writer.writerow([i,i,i])
1

What about using pandas library for creating it? Using it you can freely choose which part of DataFrame you want to save to csv.

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

df.to_csv('out.csv')

1 Comment

thankyou very much @Jakub but this will just display col1:[1,2] and col2:[3,4] I want to save data as col1: [range(20-30) increment by .2] and each number to be matched by another new value from col2:[range(2-5) increment by .5] can you guide me for this.

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.