Bottom Page

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
#11
So based on this i would assume if you make a list and append fact.effective_numeric_value or fact.normalized_value (based on whatever you want) then you would get all the values instead of the last one. Then return the list instead of factValue.

Thank you, that sounds very logical, but as I'm completely new to Python, I would not know how I can make those changes. I will try though. I assume the code that I need to modify is below?

def factFinder( instance, namespace, label ):
	# Locate facts in the instance document by namespace and label, ignoring facts that have a context with a segment_element
	l = []
	for f in instance.items:
		if f.qname.namespace_name.find( namespace ) and f.qname.local_name == label:
			l.append( f )
	if not l:
		print('### NOT FOUND', label, type(l))
	return l
	
	
def printFacts( facts, indent=1, targetDate=None ):
    # Find the fact for the relevant target date and print it
    factValue = 0
    for fact in facts:
        if targetDate==None or fact.context.period.instant == targetDate:
            if fact.concept.item_type==fact.concept.MONETARY_ITEM_TYPE:
                factValue = fact.effective_numeric_value
                print( indent * "\t", camelToSpaces( fact.qname.local_name ).ljust(100-indent*8), "$", '{0:>16,}'.format( factValue ) )
            else:
                factValue = fact.normalized_value
                print( indent * "\t", camelToSpaces( fact.qname.local_name ).ljust(100-indent*8), factValue )
    return factValue
Quote
#12
I cant really verify that this works since i dont have "altova"

def printFacts( facts, indent=1, targetDate=None ):
    my_list = []
    for fact in facts:
        if targetDate==None or fact.context.period.instant == targetDate:
            if fact.concept.item_type==fact.concept.MONETARY_ITEM_TYPE:
                my_list.append(fact.effective_numeric_value)
                #print( indent * "\t", camelToSpaces( fact.qname.local_name ).ljust(100-indent*8), "$", '{0:>16,}'.format( factValue ) )
            else:
                my_list.append(fact.normalized_value)
                #print( indent * "\t", camelToSpaces( fact.qname.local_name ).ljust(100-indent*8), factValue )
    return my_list
If you want the prints uncommented you would have to remove this {0:>16,} to just {} as thats not going to work on a list like it did a single string value
Quote
#13
Thank you very much. I updated the script with your code. I did receive the below error (which I think you were expecting to see as indicated in your earlier post)

Error:
Quote:Warning: An error occured while processing Python script 'file:///Users/ivanstamenkovic/Desktop/SEC/extractRatios.py'!
Reason: TypeError: can only concatenate list (not "str") to list
Details:
python-traceback: File "/Users/ivanstamenkovic/Desktop/SEC/extractRatios.py", line 149, in on_xbrl_valid
ratiofile.write( tradingSymbol + "," + entityRegistrantName + "," + entityCentralIndexKey + "," + currentFiscalYearEndDate + "," + docType + "," + documentPeriodEndDate + "," + documentFiscalYearFocus + "," + documentFiscalPeriodFocus + "," + entityCommonStockSharesOutstanding + "," + documentCreationDate + "," + commonStockSharesIssued + "," + treasuryStockShares + "," + commonStockSharesOutstanding + "\n")
python-error: An error occured while processing Python script 'file:///Users/ivanstamenkovic/Desktop/SEC/extractRatios.py'!


Line 149 in script is:

ratiofile.write( tradingSymbol + "," + entityRegistrantName + "," + entityCentralIndexKey + "," + currentFiscalYearEndDate + "," + docType + "," + documentPeriodEndDate + "," + documentFiscalYearFocus + "," + documentFiscalPeriodFocus + "," + entityCommonStockSharesOutstanding + "," + documentCreationDate + "," + commonStockSharesIssued + "," + treasuryStockShares + "," + commonStockSharesOutstanding + "\n")
Do you have any suggestions? I will also read the Python standard module (CSV writing 14.1) in case I can find how to fix.
Quote
#14
which version of python are you using?
In python 3
I would split line 149, and use the format statement:
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)
if python 3.6 (and newer)
        with open("ratios.csv", "a") as ratiofile:
with open("ratios.csv", "a") as ratiofile:
    buff = f'{tradingSymbol},{entityRegistrantName},{entityCentralIndexKey},{currentFiscalYearEndDate},' \
           f'{docType},{documentPeriodEndDate},{documentFiscalYearFocus},{documentFiscalPeriodFocus},' \
           f'{entityCommonStockSharesOutstanding},{documentCreationDate},{commonStockSharesIssued},' \
           f'{treasuryStockShares},{commonStockSharesOutstanding}\n'
    ratiofile.write(buff)
You don't need the close statement when using with, file is closed automatically.

(not tested- may need minor tweeking)
Quote
#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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  List Comprehension - Creating a list of the length of an item help paul41 0 54 11 hours ago
Last Post: paul41
  removing items from a list or group within a for loop. allusernametaken 9 246 Nov-13-2019, 01:58 AM
Last Post: allusernametaken
  List and loop Sandeep2000 5 199 Oct-23-2019, 08:54 AM
Last Post: perfringo
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 147 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 128 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Appending to list not working and causing a infinite loop eiger23 8 191 Oct-10-2019, 03:41 PM
Last Post: eiger23
  loop through list with do something 3Pinter 1 131 Oct-08-2019, 03:47 PM
Last Post: Gribouillis
  Creating a list from a comprehension using a list rm197 6 144 Sep-30-2019, 07:02 PM
Last Post: rm197
  Converting List into list of tuples ARV 4 322 Sep-28-2019, 04:58 AM
Last Post: perfringo
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 109 Sep-18-2019, 09:43 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)