1

I want to generate a StringIO in my tests using this list of headers and rows but my syntax creates a set of nested lists.

row = ['0ceaf6ef-35b0-4f5b-ad11-02f7c50c8387', '30 - Day Business Loan',
       '500000.00',...]
headers = ['Application UID', 'Product name', 'Amount requested',
           'Offered amount',...]
s_headers = ' '.join([str(elem) for elem in headers])
s_rows = ' '.join([str(elem) for elem in row])
self.in_mem_csv = StringIO('\n'.join([s_headers, s_rows]))

self.test_reader = reader(self.in_mem_csv, delimiter=',', quotechar='|')

Printing the first row gives me this:

print(next(self.test_reader))
["['Application UID'", " 'Product name'", " 'Amount requested'", " 'Offered amount'",...]

But my expected output is this:

['Application UID', 'Product name', 'Amount requested', 'Offered amount',...]
Anjayluh
  • 1,647
  • 1
  • 13
  • 22
  • You say "gives me" and "expected output" - can you add some code that shows how you actually obtain that output? Because your code example ends with defining the `StringIO` object, not how you actually obtain that output. – Grismar Jan 06 '22 at 22:52
  • Where did `self.headers` come from? – MYousefi Jan 06 '22 at 22:53
  • You say "printing the first row". How are you extracting and printing the first row? What you have up there does NOT produce the result you describe. For one thing, you're joining the columns with a space instead of a comma. – Tim Roberts Jan 06 '22 at 23:06
  • @TimRoberts I have added the line for reading the StringIO – Anjayluh Jan 06 '22 at 23:09
  • Yes, because you are writing out SPACE delimited fields, and you have your CSV reader looking for COMMA delimited fields. – Tim Roberts Jan 06 '22 at 23:11
  • I might ask what's the point of writing a list of rows out to an in-memory CSV and then reading it back in? You'll end up with what you started with. – Tim Roberts Jan 06 '22 at 23:13

2 Answers2

2

I think your code is working and you just don't realize it. Here, I changed your ' '.join to ','.join, and I can read your file back with the CSV module:

import csv
from io import StringIO

row = ['0ceaf6ef-35b0-4f5b-ad11-02f7c50c8387', '30 - Day Business Loan', '500000.00','150000.00']
headers = ['Application UID', 'Product name', 'Amount requested', 'Offered amount']
s_headers = ','.join(headers)
s_rows = ','.join(row)
in_mem_csv = StringIO('\n'.join([s_headers, s_rows]))

for row in csv.reader(in_mem_csv):
    print(row)

Output:

['Application UID', 'Product name', 'Amount requested', 'Offered amount', 'Ellipsis']
['0ceaf6ef-35b0-4f5b-ad11-02f7c50c8387', '30 - Day Business Loan', '500000.00', '150000.00']
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
2

I think this is what's needed. It converts each element of the list into a double-quoted string, then joins them together with a comma.

from io import StringIO

row = ['0ceaf6ef-35b0-4f5b-ad11-02f7c50c8387', '30 - Day Business Loan',
       '500000.00',]
headers = ['Application UID', 'Product name', 'Amount requested',
           'Offered amount',]

s_headers = ','.join([f'"{elem}"' for elem in headers])
s_rows = ','.join([f'"{elem}"' for elem in row])

in_mem_csv = StringIO('\n'.join([s_headers, s_rows]))

print(in_mem_csv.getvalue())

Output

"Application UID","Product name","Amount requested","Offered amount"
"0ceaf6ef-35b0-4f5b-ad11-02f7c50c8387","30 - Day Business Loan","500000.00"
martineau
  • 119,623
  • 25
  • 170
  • 301