我正在写一个脚本从txt文件中提取特征。 我创建了第一个包含所需列名的数据帧。 然后,我循环遍历文件夹中的所有文件,并提取所需的功能。 然后我创建一个新的数据框,它是一行,并将它附加到“features”数据框中。你知道吗
我没有得到错误,但是“features”数据帧保持为空。 在创建df时,我对索引有点困惑,因为我认为错误可能来自那里。但如果你不知道呢?或者不想在追加时迭代索引? 难道没有一种方法可以像Java中的集合(Arraylist等)那样“添加”行吗?你知道吗
#we create a df that will contain the chosen features
features = pd.DataFrame(columns =
{
"salary_start",
"salary_end",
"open date",
"requirements",
"duties",
"deadline",
"selection"
})
for file_name in list_of_files:
header = []
try:
f = open(path_to_plain_txt + file_name, "r")
file = f.read().replace('\t', '')
text = file.replace('\n', '')
headers = [head for head in file.split("\n") if head.isupper()]
#since I'm using a utils file, there's no confusion between re
variables and the csv.
salare = re.search(utils.salary, text)
date = datetime.strptime(re.search(utils.opendate, text).group(3),
'%m-%d-%y')
duties = re.search(utils.duties, text).group(2)
try:
requirements = re.search(utils.requirements, text).group(2)
except Exception as e:
requirements =
re.search('(.*)NOTES?',re.findall(r'(REQUIREMENTS?)(.*)
(NOTES?)',text)[0][1][:1200]).group(1)
try:
enddate = datetime.strptime(re.search(re.enddate,text).group(),
'%m-%d-%y')
except Exception as e:
enddate = np.nan
selection = [z[0] for z in re.findall('([A-Z][a-z]+)((\s\.\s)+)',
text)]
line = pd.DataFrame({'salary_start': [salare.group(1)],
'salara_end': [salare.group(5)],
'open date': [date],
'requirements': [requirements],
'duties': [duties],
'deadline': [enddate],
'selection': [selection]
})
features.append(line, ignore_index = True)
#and now we write everything in the CSV
except Exception as e:
print(e)
我解决了这个问题。 所以我从中学到了两件事:
1-不同语言的集合工作方式不同=>;没有假设!你知道吗
2-多次重读文档绝不是浪费时间。你知道吗
熊猫文献说:
为了使代码正常工作,我必须这样做:
相关问题 更多 >
编程相关推荐