Home > database >  adding array into multiple column in python
adding array into multiple column in python

Time:10-24

I have an array that I want to append as multiple columns what I am getting

what I want

pages = [1,2,3,4,5,6]
with open(nlnewsdata, 'a',newline='') as nlnews:
wr = csv.writer(nlnews, quoting=csv.QUOTE_ALL)
wr.writerow(['Daily Journal (Park Hills, MO)',"Jan","2003",result,pages])

now the output Im expecting is like this

Daily Journal (Park Hills, MO), Jan , 2003, 6, 1,2,3,4,5,6

what Im getting is

Daily Journal (Park Hills, MO), Jan,2003,6,1 2 3 4 5 6

I want to split the array into different columns not all the pages in a single column
I have tried np.savetext and pages.tofile but the thing is I am adding some data row by row and on the same run I want this array to split across multiple columns too.

CodePudding user response:

You can use * to unpack the list

wr.writerow(['Daily Journal (Park Hills, MO)',"Jan","2003",result,*pages])
  • Related