[英]How do I solve 404 Flask error in python?
I'm dealing with text files and new in learning API building with Flask, I wanted to have data extraction from text files, and would like to generate the JSON output, but unfortunately, i keep getting this 404 page when I load the 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)
而output屏是
您需要在 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)```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.