Python Forum
How to change from printFacts ( ) to return a list & Loop over list when writing CSV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change from printFacts ( ) to return a list & Loop over list when writing CSV
#15
Quote:ratiofile.write( tradingSymbol + "," + entityRegistrantName + "," + entityCentralIndexKey + "," + currentFiscalYearEndDate + "," + docType + "," + documentPeriodEndDate + "," + documentFiscalYearFocus + "," + documentFiscalPeriodFocus + "," + entityCommonStockSharesOutstanding + "," + documentCreationDate + "," + commonStockSharesIssued + "," + treasuryStockShares + "," + commonStockSharesOutstanding + "\n")
I would also write this as Larz suggested (most likely the first one since the latter is newer). The author looks like they came from C/C++ and not much python.

Anyways, this line is all concatenating strings and values that are strings. So now that it returns a list, one (or many) of those now might be a list. I can see at least one of them is as one is the return value of the function printFacts *docType"

        docType = printFacts( documentType )
        entityName = printFacts( entityRegistrantName )
        entityCIK = printFacts( entityCentralIndexKey )
doctype would now be a list, not a string anymore as printFacts returns lists now


Its going to mess up the layout of the csv file. Im not sure what way you want it. The following would convert the list values to a string joined by a comma , via the first line in the code below. For every value that is now a list would have to be done such as this. Now sure how many as they are all on the same line.
docType = ','.join(docType)
with open("ratios.csv", "a") as ratiofile:
    buff = '{},{},{},{},{},{},{},{},{},{},{},{},{}\n'.format(tradingSymbol, entityRegistrantName, entityCentralIndexKey,
                                                          currentFiscalYearEndDate,
                                                          docType, documentPeriodEndDate, documentFiscalYearFocus,
                                                          documentFiscalPeriodFocus,
                                                          entityCommonStockSharesOutstanding, documentCreationDate,
                                                          commonStockSharesIssued, treasuryStockShares,
                                                          commonStockSharesOutstanding)
    ratiofile.write(buff)
Recommended Tutorials:
Reply


Messages In This Thread
RE: How to change from printFacts ( ) to return a list & Loop over list when writing CSV - by metulburr - Aug-30-2017, 12:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 292 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,715 Jan-05-2024, 08:30 PM
Last Post: sgrey
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,175 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Change font in a list or tuple apffal 4 2,699 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  Delete strings from a list to create a new only number list Dvdscot 8 1,547 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 922 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,851 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,521 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,006 Aug-24-2022, 05:26 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,570 Aug-06-2022, 11:39 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020