1

I'm trying to save the following list to a CSV file:

import csv

list = ['52.26.17.68', 'TotalPhysicalMemory : 4294361088', '10.12.15.14', 'TotalPhysicalMemory : 6546534654']

with open('testing.csv', 'w', newline='') as r:
        writer = csv.writer(r, delimiter='\t')
        writer.writerows(list)

With this my CSV file looks like:

How can I get "TotalPhysicalMemory" into column B so it looks like below?

enter image description here

Georgy
  • 12,464
  • 7
  • 65
  • 73
Posteingang
  • 53
  • 1
  • 8
  • Also: [How to print several array elements per line to text file](https://stackoverflow.com/q/14941854/7851470) – Georgy Nov 29 '19 at 11:43

1 Answers1

1

Use zip with list slicing

Ex:

import csv

lst = ['52.26.17.68', 'TotalPhysicalMemory : 4294361088', '10.12.15.14', 'TotalPhysicalMemory : 6546534654']

with open('testing.csv', 'w', newline='') as r:
    writer = csv.writer(r, delimiter='\t')
    writer.writerows(zip(lst[::2], lst[1::2]))
Rakesh
  • 81,458
  • 17
  • 76
  • 113