我是Python新手(使用Python 3.6)。我有一个包含公司信息的read.txt文件。文件开始与不同的报告特性f.readline与f.read打印输出
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.readlines(),然后对行进行换行而不是按\ n分割,因为这可能不会给您预期的结果。 – Ajurna
我不确定第一个代码片段甚至值得一提的建议,第二种方式显然是要走的路 –
好点。放弃了第一种方法。 – CoryKramer