简体   繁体   English

f.readline与f.read打印输出

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

I am new to Python (using Python 3.6). 我是Python新手(使用Python 3.6)。 I have a read.txt file containing information about a firm. 我有一个read.txt文件,其中包含有关公司的信息。 The file starts with different report characteristics 该文件以不同的报告特征开头

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

I am trying to extract both dates (['20120928','20121128']) as well as some strings that are in the text (ie if the string exists, then I want a '1'). 我试图提取两个日期(['20120928','20121128'])以及文本中的一些字符串(即如果字符串存在,那么我想要'1')。 Ultimately, I want a vector giving me both dates + the 1s and 0s of different strings, that is, something like: ['20120928','20121128','1','0']. 最终,我想要一个向量给我两个日期+不同字符串的1和0,即:['20120928','20121128','1','0']。 My code is the following: 我的代码如下:

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)

If I run this code, I obtain ['1','0'], omitting the dates and giving correct reads of the file, var1 exists (ok '1') and var2 does not (ok '0'). 如果我运行此代码,我获得['1','0'],省略日期并给出正确的文件读取,var1存在(ok'1')而var2不存在(ok'0')。 What I don't understand is why it doesn't report the dates. 我不明白的是为什么它不报告日期。 Importantly, when I change line2 to "line2=f.readline()", then I obtain ['20120928','20121128','0','0']. 重要的是,当我将line2更改为“line2 = f.readline()”时,我获得['20120928','20121128','0','0']。 Ok with the dates now, but I know that var1 exists, it seems it doesn't read the rest of the file? 好了现在的日期,但我知道var1存在,它似乎不读取文件的其余部分? If I omit "line2=f.read()", it spits out a vector of 0s for each line, except for my desired output. 如果我省略“line2 = f.read()”,它会为每一行吐出一个0的向量,除了我想要的输出。 How can I omit these 0s? 我怎么能省略这些0?

My desired output would be: ['20120928','20121128','1','0'] 我想要的输出是:['20120928','20121128','1','0']

Sorry for bothering. 很抱歉打扰。 Thank you anyway! 还是要谢谢你!

The line f.read() will read the entire file into the variable line2 . f.read()将整个文件读入变量line2 If you want to read line by line you could skip the f.read() all together and just iterate like so 如果你想逐行阅读,你可以一起跳过f.read()并像这样迭代

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

Otherwise as written, after you .read() into line2 there is no more text to read out of f as it is all contained in the line2 variable. 否则按照写入,在.read()line2之后,没有更多的文本可以读出f因为它全部包含在line2变量中。

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

The way I went through it was finally the following: 我经历过的方式终于如下:

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)

So far this is what I got. 到目前为止,这就是我所得到的。 It worked for me, although I guess working with beautifulsoup would increase the efficiency of the code. 虽然我认为使用beautifulsoup可以提高代码的效率,但它对我有用。 Next step :) 下一步 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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