-2

I am generating a csv which contains the results I expect, all the numbers are right.

However, presentation wise it contains parentheses around everything and comma's etc.

Is there a way I can remove these?

I tried adding comma as a delimiter but that didn't solve it.

Example output:

Sure: ('Egg',)

results = [] 
results1 = []
results2 = []
results3 = []
results4 = []
results5 = []
results6 = []

cur.execute(dbQuery)

results.extend(cur.fetchall())

cur.execute(dbQuery1)

results1.extend(cur.fetchall())

cur.execute(dbQuery2)

results2.extend(cur.fetchall())

cur.execute(dbQuery3)

results3.extend(cur.fetchall())

cur.execute(dbQuery4)

results4.extend(cur.fetchall())

cur.execute(dbQuery5)

results5.extend(cur.fetchall())

cur.execute(dbQuery6)

results6.extend(cur.fetchall())


with open("Stats.csv", "wb") as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(['Query1', 'Query2', 'Query3', 'Query4', 'Query5', 'Query6', 'Query7'])
    csv_writer.writerows(zip(*[results, results1,  results2, results3, results4, results5, results6]))
user2320010
  • 93
  • 1
  • 8
  • 1
    Could you add the output you get and example of your desired output? – Rithin Chalumuri Nov 06 '19 at 10:52
  • So the code does not write the header `Query1`, `Query2`, ecc? Also, do you realize that `zip(*[A, B, C])` is just `zip(A, B, C)` with extra steps? The point of `*` is to unpack an iterable where you do not know the number of elements like `zip(*some_iterable)`. In your case you are just adding syntax for nothing. – Giacomo Alzetta Nov 06 '19 at 10:56

1 Answers1

1

The zip function is returning a list of tuples [(x, y), (t, s)...]

The writerows method expects a list of lists. So, I think you should format the zip return before call the writerows. Something like that should work:

result = zip(results, results1,  results2, results3, results4, results5, results6)
csv_writer.writerows([list(row) for row in result])

EDIT:

I think I understood the problem you are having here (so ignore my previous answer above).

The fetchall function is returning a list of tuples like [(x,), (y,)]

So, then your resultsX variables will have this format. Then, you are applying a zip between these lists (see here what zip does).

If for example we have results = [(x,), (y,)] results1 = [(t,), (z,)]

When you run the zip(results, results1), it will return: [((x,), (t,)), ((y,), (z,))]

So, that's the format of the list you are passing to the writerows, which means the first row will be: ((x,), (t,)) where the element one is: (x,) and the second one is (t,)

So, not sure what you are expecting to write in the CSV with the zip function. But the result you are getting is because your elements to write in the csv are tuples instead of values.

I don't know the query you are doing here, but if you are expecting just one field per each result, maybe then you need to strip out the tuple in each resultsX variable. You can take a look how to do it in this thread: https://stackoverflow.com/a/12867429/1130381

I hope it helps, but that's the best I can do with the info you provided.

edgarzamora
  • 1,472
  • 1
  • 9
  • 17
  • Hi, when i use this it still outputs the comma's etc – user2320010 Nov 06 '19 at 11:21
  • Can you provide us with the next information: - An example of the content of one of the result list. - An example of a current row wrote on the CSV. - An example of the expected row write in the CSV. Thanks – edgarzamora Nov 06 '19 at 11:38
  • Sure, here is an example: ('Goose',) – user2320010 Nov 06 '19 at 11:42
  • This is an example of what? I asked for 3 different examples, so please could you give me more accurate information. Also, is this `('Goose', )` tuple in a list? or that's the output on the CSV? – edgarzamora Nov 06 '19 at 11:45
  • That is one of the tuples that is being pulled from my SQL queries, and it displays in the CSV that way. So in the DB it looks like "Goose", it looks like ('Goose',) within the CSV file and I would prefer if the CSV displayed it like Goose with no quotes etc – user2320010 Nov 06 '19 at 11:46
  • Okay, that's not really helpful, because I still not know what you are passing to the `writerows` function. So, maybe to allow us to be more helpful, can you add the next line: `result = zip(results, results1, results2, results3, results4, results5, results6)`, then `print(list(result))` and post in your question what this print is returning? – edgarzamora Nov 06 '19 at 11:53