Home > Blockchain >  How to add a long list of headers and rows to a StringIO
How to add a long list of headers and rows to a StringIO

Time:01-07

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',...]

CodePudding user response:

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']

CodePudding user response:

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"
  •  Tags:  
  • Related