简体   繁体   中英

Read a text file with Python

I have a text file with two million rows:

adsh    tag version coreg   ddate   qtrs    uom value   footnote
0001065088-20-000018    DecreaseInUnrecognizedTaxBenefitsIsReasonablyPossible   us-gaap/2018        20200331    0   USD 19000000.0000   
0000885725-20-000022    DecreaseInUnrecognizedTaxBenefitsIsReasonablyPossible   us-gaap/2018        20200331    0   USD 98000000.0000   
0000837465-20-000010    DecreaseInUnrecognizedTaxBenefitsIsReasonablyPossible   us-gaap/2018        20200331    0   USD 449000.0000 
0001730168-20-000109    DecreaseInUnrecognizedTaxBenefitsIsReasonablyPossible   us-gaap/2018        20200430    0   USD 154000000.0000  
0001730168-20-000109    OperatingLeasesRentExpenseNet   us-gaap/2018        20190430    1   USD 59000000.0000   
0001730168-20-000109    OperatingLeasesRentExpenseNet   us-gaap/2018        20190430    2   USD 126000000.0000  
0001104659-20-068703    OperatingLeasesRentExpenseNet   us-gaap/2018        20180131    4   USD 19700000.0000
0001418135-20-000018    OtherAmortizationOfDeferredCharges  us-gaap/2018        20190331    1   USD 36000000.0000   Primarily includes amortization of customer rebates and upfront payments.
0001002910-20-000115    OtherAmortizationOfDeferredCharges  us-gaap/2018    UnionElectricCompany    20200331    1   USD 23000000.0000

You can see that this text file has 9 columns and, in the first rows there are only 7 columns. This is because there are other rows with 9 columns. I need to write a MySQL database with every row but I have to take into account the NULL values like this:

adsh                    tag                                                     version       coreg                  ddate      qtrs    uom  value          footnote
0001065088-20-000018    DecreaseInUnrecognizedTaxBenefitsIsReasonablyPossible   us-gaap/2018   NULL                  20200331   0       USD  19000000.0000  NULL
0001002910-20-000115    OtherAmortizationOfDeferredCharges                      us-gaap/2018   UnionElectricCompany  20200331   1       USD  23000000.0000  NULL

How can I make it?

with open ("filename.txt", 'r') as nametxt; Text = (nametext (read()); Print (Text)

Something like that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM