0

I have been writing a script that fetches data from 3 external sources.

def fetch1():
    return array
def fetch2():
    return array2
def fetch3():
    return array3

All the elements of list are associated with each other. i.e. array[0],array2[0],array3[0] should be put together in a csv.

Some of the elements of array2 might be blank.

Now I want to write all of them in a csv file. Example of arrays :

array = ["dog", "cat", "horse", "lion"]
array2 = ["500USD", "300USD", "900USD", ""]
array3 = ["Buy", "Sell", "Buy", "Buy"]

And how to write them in a way that every time I start this script, data should be entered after the data that was previously entered, instead of making a new file.

What I have tried so far:

I have created a dictionary of array and array2 because of the fact that array2 elements can be null. Then I have written the following function.

def write():
   writer = csv.writer(open('dict.csv', 'wb'))
   for key, value in arrayNarray2.items():
       writer.writerow([key.encode("UTF-8"), value.encode("UTF-8")])
Sanidhay
  • 159
  • 12

2 Answers2

2

Is this the output format you want?

"dog", "500USD", "Buy"
"cat", "300USD", "Sell"
...

If so then you can use zip to combine all of the first items in your lists, then all of the second items, etc:

from itertools import izip_longest

with open('dict.csv', 'ab') as out:
    writer = csv.writer(out)
    for row in izip_longest(array, array2, array3):
        writer.writerow([x.encode("utf-8") for x in row])

I've used izip_longest because you said the lists may not be the same length, so it will fill in None for anything missing.

BenC
  • 451
  • 3
  • 7
  • Thanks, but is there a way where I can use a dictionary for the first two arrays? And leave array3 as it is. Because the serial numbers will be common for all the three things. – Sanidhay Dec 21 '15 at 08:20
  • @Sanidhay What does your dict look like? I'm not totally sure what you want your inputs and outputs to be, or how you're combining the first two arrays. – BenC Dec 21 '15 at 08:28
  • I have improvised my code according to your answer. I am getting my desired output, thanks @BenC – Sanidhay Dec 21 '15 at 08:31
1

you have to open your file in append mode rather than write mode.

Bug in code:

writer = csv.writer(open('dict.csv', 'wb'))

Fixed version:

writer = csv.writer(open('dict.csv', 'ab'))

The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):

 ``r''   Open text file for reading.  The stream is positioned at the
         beginning of the file.

 ``r+''  Open for reading and writing.  The stream is positioned at the
         beginning of the file.

 ``w''   Truncate file to zero length or create text file for writing.
         The stream is positioned at the beginning of the file.

 ``w+''  Open for reading and writing.  The file is created if it does not
         exist, otherwise it is truncated.  The stream is positioned at
         the beginning of the file.

 ``a''   Open for writing.  The file is created if it does not exist.  The
         stream is positioned at the end of the file.  Subsequent writes
         to the file will always end up at the then current end of file,
         irrespective of any intervening fseek(3) or similar.

 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  The stream is positioned at the end of the file.  Subse-
         quent writes to the file will always end up at the then current
         end of file, irrespective of any intervening fseek(3) or similar.

The above content copied from python open built-in function: difference between modes a, a+, w, w+, and r+?

Community
  • 1
  • 1
Elixir Techne
  • 1,848
  • 15
  • 20