Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write list of list to CSV?
#11
the only problem I see is that dlist is already list of lists. But this will not prevent writing to file (although it may be not what you want:
Output:
device1,HundredGigE0/2/0/0,"['ALPHANUMERICWITHCHARAC;;;', 'ALPHANUMERICWITHCHARAC;;;', 'ALPHANUMERICWITHCHARAC;;;']" ,HundredGigE0/2/0/0.242, ,HundredGigE0/2/0/0.244 l2transport, ,HundredGigE0/2/0/1, ,HundredGigE0/2/0/0.244, device1,TenGigabitEthernet1/1/0.138,['ALPHANUMERICWITHCHARAC;;;'] device1,TenGigE0/1/0/15,['ALPHANUMERICWITHCHARAC;;;']
note that lst column is str repr of a list.
If you make it like this

import itertools
import csv
opo = ['device1', 'device1', 'device1']
intfname = [['HundredGigE0/2/0/0', 'HundredGigE0/2/0/0.242', 'HundredGigE0/2/0/0.244 l2transport', 'HundredGigE0/2/0/1', 'HundredGigE0/2/0/0.244'], ['TenGigabitEthernet1/1/0.138'], ['TenGigE0/1/0/15']]
descname = [['ALPHANUMERICWITHCHARAC;;;', 'ALPHANUMERICWITHCHARAC;;;', 'ALPHANUMERICWITHCHARAC;;;'], ['ALPHANUMERICWITHCHARAC;;;'], ['ALPHANUMERICWITHCHARAC;;;']]# ['0/2/0/0info1', '0/2/0/0.242info2', '0/2/0/0.244info3']

opo = [[ele] for ele in opo]
 
## EXPORT AS CSV
with open('intlist.csv', 'w', newline='') as updatecsv:
    write = csv.writer(updatecsv, delimiter=',',lineterminator='\n')
    for cols in zip(opo,intfname,descname):
        rows = itertools.zip_longest(*cols, fillvalue='')
        write.writerows(rows)
updatecsv.close()
print ("Done...")
the output will be
Output:
device1,HundredGigE0/2/0/0,ALPHANUMERICWITHCHARAC;;; ,HundredGigE0/2/0/0.242,ALPHANUMERICWITHCHARAC;;; ,HundredGigE0/2/0/0.244 l2transport,ALPHANUMERICWITHCHARAC;;; ,HundredGigE0/2/0/1, ,HundredGigE0/2/0/0.244, device1,TenGigabitEthernet1/1/0.138,ALPHANUMERICWITHCHARAC;;; device1,TenGigE0/1/0/15,ALPHANUMERICWITHCHARAC;;;
Note that desc values go to first three lines, I don't know if this is correct

What I try to say is if the print output is OK, then second part should work too (i.e. the lists are populated with values, not empty)

you can make opo list of lists by design, when you create it in the first place.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
Hi, yes I have removed the list of list convertion for dlist a while ago.

Yes im getting the correct output when I isolate(recreate onlt to twst the output).

Not sure now wheather theres a code preventing to write properly.. Thanks
Reply
#13
(Feb-24-2019, 09:33 AM)searching1 Wrote: Not sure now wheather theres a code preventing to write properly.. Thanks
I don't see anything like this
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
(Feb-24-2019, 09:37 AM)buran Wrote:
(Feb-24-2019, 09:33 AM)searching1 Wrote: Not sure now wheather theres a code preventing to write properly.. Thanks
I don't see anything like this

Yes, a little bit odd. though will try to search for solution. Not sure if there a proplem with my ubuntu server since the data was pullout from there.

But I have checked the permission etc from ubuntu and seem like its working.
Reply
#15
(Feb-24-2019, 06:41 PM)searching1 Wrote: Yes, a little bit odd. though will try to search for solution. Not sure if there a proplem with my ubuntu server since the data was pullout from there.

But I have checked the permission etc from ubuntu and seem like its working.

But you said that the print lines work fine (i.e. you get the lists you want)
Also you say that you can write if separate the second part (i.e. no problem with permissions to write)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
My girlfrend often needs to convert different pdf-files. I want to help her with this. Found the picture converter online onlineconvert
Reply
#17
@buran, its now working... hooooo :) problem is with the current location of my directory wherein the script pullout/move to different directory.. so I have to put the absolute path when saving on CSV.

Thanks for your help man.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,182 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,552 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 923 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,855 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,578 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,307 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 1,846 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 4,056 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,169 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,125 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020