簡體   English   中英

f.readline與f.read打印輸出

[英]f.readline versus f.read print output

我是Python新手(使用Python 3.6)。 我有一個read.txt文件,其中包含有關公司的信息。 該文件以不同的報告特征開頭

CONFORMED PERIOD REPORT:             20120928 #this is 1 line
DATE OF REPORT:                      20121128 #this is another line

and then starts all the text about the firm..... #lots of lines here

我試圖提取兩個日期(['20120928','20121128'])以及文本中的一些字符串(即如果字符串存在,那么我想要'1')。 最終,我想要一個向量給我兩個日期+不同字符串的1和0,即:['20120928','20121128','1','0']。 我的代碼如下:

exemptions = [] #vector I want

with open('read.txt', 'r') as f:
    line2 = f.read()  # read the txt file
    for line in f:
        if "CONFORMED PERIOD REPORT" in line:
            exemptions.append(line.strip('\n').replace("CONFORMED PERIOD REPORT:\t", ""))  # add line without stating CONFORMED PERIOD REPORT, just with the date)
        elif "DATE OF REPORT" in line:
            exemptions.append(line.rstrip('\n').replace("DATE OF REPORT:\t", "")) # idem above

    var1 = re.findall("string1", line2, re.I)  # find string1 in line2, case-insensitive
    if len(var1) > 0:  # if the string appears, it will have length>0
        exemptions.append('1')
    else:
        exemptions.append('0')
    var2 = re.findall("string2", line2, re.I)
    if len(var2) > 0:
        exemptions.append('1')
    else:
        exemptions.append('0')

print(exemptions)

如果我運行此代碼,我獲得['1','0'],省略日期並給出正確的文件讀取,var1存在(ok'1')而var2不存在(ok'0')。 我不明白的是為什么它不報告日期。 重要的是,當我將line2更改為“line2 = f.readline()”時,我獲得['20120928','20121128','0','0']。 好了現在的日期,但我知道var1存在,它似乎不讀取文件的其余部分? 如果我省略“line2 = f.read()”,它會為每一行吐出一個0的向量,除了我想要的輸出。 我怎么能省略這些0?

我想要的輸出是:['20120928','20121128','1','0']

很抱歉打擾。 還是要謝謝你!

f.read()將整個文件讀入變量line2 如果你想逐行閱讀,你可以一起跳過f.read()並像這樣迭代

with open('read.txt', 'r') as f:
    for line in f:

否則按照寫入,在.read()line2之后,沒有更多的文本可以讀出f因為它全部包含在line2變量中。

line2 = f.read()整個文件讀入line2 ,因此for line in f: loop中沒有什么可以讀取你的for line in f:

我經歷過的方式終於如下:

exemptions = [] #vector I want

with open('read.txt', 'r') as f:
    line2 = "" # create an empty string variable out of the "for line" loop
    for line in f:
        line2 = line2 + line #append each line to the above created empty string
        if "CONFORMED PERIOD REPORT" in line:
            exemptions.append(line.strip('\n').replace("CONFORMED PERIOD REPORT:\t", ""))  # add line without stating CONFORMED PERIOD REPORT, just with the date)
        elif "DATE OF REPORT" in line:
            exemptions.append(line.rstrip('\n').replace("DATE OF REPORT:\t", "")) # idem above

    var1 = re.findall("string1", line2, re.I)  # find string1 in line2, case-insensitive
    if len(var1) > 0:  # if the string appears, it will have length>0
        exemptions.append('1')
    else:
        exemptions.append('0')
    var2 = re.findall("string2", line2, re.I)
    if len(var2) > 0:
        exemptions.append('1')
    else:
        exemptions.append('0')

print(exemptions)

到目前為止,這就是我所得到的。 雖然我認為使用beautifulsoup可以提高代碼的效率,但它對我有用。 下一步 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM