我正在处理文本文件以及Flask学习API构建的新功能,我想从文本文件中提取数据,并希望生成JSON输出,但是不幸的是,加载时我一直得到这个404页面API URL谁能帮助我 预先感谢
这是下面的代码
import re
from flask import Flask
from flask import request
import json
app= Flask(__name__)
@app.route('/flask_resume',methods =["POST"])
def flask_resume():
file = request.files['file']
files = file.read()
return files
#extracting workexperience and summary titles
with open(files,'r', encoding='latin-1')as file1:
with open('Details.txt','r',encoding='latin-1' )as file2:
same = set(file1).intersection(file2)
same.discard('\n')
words_list = []
for line in same:
words_list.append(line)
words_list = list(map(str.strip,words_list))
print ('words_list', words_list)
#extracting other titles
with open(files,'r', encoding='latin-1')as file3:
with open('other_details.txt','r',encoding='latin-1' )as file4:
same1 = set(file3).intersection(file4)
same1.discard('\n')
words_extract = []
for f in same1:
words_extract.append(f)
words_extract = list(map(str.strip,words_extract))
print ('words_extract', words_extract)
#function to replace extracted titles
def multiwordReplace(text, wordDic):
rc = re.compile('|'.join(map(re.escape, wordDic)))
def translate(match):
return wordDic[match.group(0)]
return rc.sub(translate, text)
str1 = open(files,'r', encoding='latin-1')
str1 = str1.read()
wordDic1 = dict((k,'Summary') for k in words_list)
wordDic2 = dict((k,'xyz') for k in words_extract)
wordDic = dict(wordDic1, **wordDic2)
print(wordDic)
with open ('set.txt','w', encoding='latin-1') as infile:
str2 = multiwordReplace(str1,wordDic)
infile.write(str2)
#extracting summary paragraphs
with open("set.txt", encoding='latin-1')as infile,open("fgl.txt",'w', encoding='latin-1')as outfile:
copy = False
for word in words_extract:
for line in infile:
if line.strip() == "Summary":
copy = True
elif line.strip() == "xyz":
copy = False
elif copy:
outfile.write(line)
if __name__ == "__main__":
app.run(debug=True)
输出屏幕是
答案 0 :(得分:0)
您需要在URL-http://127.0.0.1:5000/中定义根路径“ /”
在if __name__ == "__main__":
上方添加以下代码
@app.route('/')
def hello():
return "hello"
如果您希望flask_resume
是您的着陆页,则应该使用'/'
而不是'/flask_resume'
。
完整代码-
import re
from flask import Flask
from flask import request
import json
app= Flask(__name__)
@app.route('/flask_resume',methods =["POST"])
def flask_resume():
file = request.files['file']
files = file.read()
return files
#extracting workexperience and summary titles
with open(files,'r', encoding='latin-1')as file1:
with open('Details.txt','r',encoding='latin-1' )as file2:
same = set(file1).intersection(file2)
same.discard('\n')
words_list = []
for line in same:
words_list.append(line)
words_list = list(map(str.strip,words_list))
print ('words_list', words_list)
#extracting other titles
with open(files,'r', encoding='latin-1')as file3:
with open('other_details.txt','r',encoding='latin-1' )as file4:
same1 = set(file3).intersection(file4)
same1.discard('\n')
words_extract = []
for f in same1:
words_extract.append(f)
words_extract = list(map(str.strip,words_extract))
print ('words_extract', words_extract)
#function to replace extracted titles
def multiwordReplace(text, wordDic):
rc = re.compile('|'.join(map(re.escape, wordDic)))
def translate(match):
return wordDic[match.group(0)]
return rc.sub(translate, text)
str1 = open(files,'r', encoding='latin-1')
str1 = str1.read()
wordDic1 = dict((k,'Summary') for k in words_list)
wordDic2 = dict((k,'xyz') for k in words_extract)
wordDic = dict(wordDic1, **wordDic2)
print(wordDic)
with open ('set.txt','w', encoding='latin-1') as infile:
str2 = multiwordReplace(str1,wordDic)
infile.write(str2)
#extracting summary paragraphs
with open("set.txt", encoding='latin-1')as infile,open("fgl.txt",'w', encoding='latin-1')as outfile:
copy = False
for word in words_extract:
for line in infile:
if line.strip() == "Summary":
copy = True
elif line.strip() == "xyz":
copy = False
elif copy:
outfile.write(line)
@app.route('/')
def hello():
return "hello"
if __name__ == "__main__":
app.run(debug=True)```