codaamok
codaamok

Reputation: 747

Python list write to CSV without the square brackets

I have this main function:

def main():
    subprocess.call("cls", shell=True)
    ipList,hostList,manfList,masterList,temp = [],[],[],[],[]
    ipList,hostList,manfList, = getIPs(),getHosts(),getManfs()
    entries = len(hostList)
    i = 0
    for i in xrange(i, entries):
        temp = [[hostList[i]],[manfList[i]],[ipList[i]]]
        masterList.append(temp) 
    with open("output.csv", "wb") as f:
        writer = csv.writer(f, delimiter=',')
        writer.writerows(masterList)

My current output is that it successfully writes to CSV but my objective is to remove the square brackets.

I tried using .join() method however I understand that it only takes single lists and not nested lists.

How can I achieve this given that I'm using a 3 dimensional list? Note, I intend to add more columns of data in the future.


Edit:

My current output for 1 row is similar to:

['Name1,'] ['Brand,'] ['1.1.1.1,']

I would like it to be:

Name1, Brand, 1.1.1.1,

Upvotes: 3

Views: 16800

Answers (4)

Syntactic Fructose
Syntactic Fructose

Reputation: 20084

What you could do is strip a string of the data maybe?

import string
writer.writerows(str(masterList).translate(string.maketrans('', ''), '[]\'')

E.g.

>>> import string
>>> temp = [['1.1.1'], ['Name1'], ['123']]
>>> str(temp).translate(string.maketrans('', ''), '[]\'')
'1.1.1, Name1, 123'

In Python 3.6:

>>> temp = [['1.1.1'], ['Name1'], ['123']]
>>> str(temp).translate({ord('['): '', ord(']'): '', ord('\''): ''})
'1.1.1, Name1, 123'

Upvotes: 1

Ihor Husar
Ihor Husar

Reputation: 86

I agree with the answers above, about the brackets removal, however if this is crucial to you for some reason, here is a function that takes a list as an input and returns you a csv row acceptable list.

def output_list(masterList):
    output = []
    for item in masterList:
        if isinstance(item,list): #if item is a list
            for i in output_list(item): #call this function on it and append its each value separately. If it has more lists in it this function will call itself again
                output.append(i)
        else:
            output.append(item)
    return output

You can use it in the line masterList.append(temp) as masterList.append(output_list(temp)), or even like this:

#in the end
with open("output.csv", "wb") as f:
    writer = csv.writer(f, delimiter=',')
    for i in masterList:
        writer.writerow(output_list(i))

Upvotes: 0

King Of Fools
King Of Fools

Reputation: 3

Try to change this:

temp = [[hostList[i]],[manfList[i]],[ipList[i]]]

to this:

temp = [hostList[i],manfList[i],ipList[i]]

Upvotes: 0

Edward Samuel Pasaribu
Edward Samuel Pasaribu

Reputation: 3968

Try to remove bracket for values in temp while creating masterList, because it will be nested list. So, the code should be:

def main():
    subprocess.call("cls", shell=True)
    ipList,hostList,manfList,masterList,temp = [],[],[],[],[]
    ipList,hostList,manfList, = getIPs(),getHosts(),getManfs()
    entries = len(hostList)
    i = 0
    for i in xrange(i, entries):
        temp = [hostList[i], manfList[i], ipList[i]]
        masterList.append(temp) 
    with open("output.csv", "wb") as f:
        writer = csv.writer(f, delimiter=',')
        writer.writerows(masterList)

Upvotes: 2

Related Questions