我按照这里找到的代码,把它放到PyCharm中的一个项目中,但是被困在f = open(input_file, 'r')行上,说找不到我的sowpods.txt文件:(
现在当我从终端尝试代码时,它将实际运行!:(不过,我正在为我的项目建立一个接口,所以希望尽快离开终端。在
文本文件在正确的位置
问题代码,有什么想法吗?在from __future__ import print_function
from flask import Flask
import string
import sys
import sqlite3 as sqlite
app = Flask(__name__)
def test_for_db():
# test for existance of sowpods database
pass
def test_for_sowpods():
# test for existence of sowpods text file
pass
def word_score(input_word):
# score the word
# need to account for the blanks
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
word_score = 0
for letter in input_word:
word_score = word_score + scores[letter]
return word_score
def word_list(input_file):
# create a list of tuples which containing the word, it's length, score and sorted value
sp_list = []
f = open(input_file, 'r')
for line in f:
sp_word = line.strip().lower()
sp_list.append((sp_word, len(sp_word), ''.join(sorted(sp_word)), word_score(sp_word)))
f.close()
return sp_list
def load_db(data_list):
# create database/connection string/table
conn = sqlite.connect("sowpods.db")
cursor = conn.cursor()
# create a table
tb_create = """CREATE TABLE spwords
(sp_word text, word_len int, word_alpha text, word_score int)
"""
conn.execute(tb_create)
conn.commit()
# Fill the table
conn.executemany("insert into spwords(sp_word, word_len, word_alpha, word_score) values (?,?,?,?)", data_list)
conn.commit()
# Print the table contents
for row in conn.execute("select sp_word, word_len, word_alpha, word_score from spwords"):
print (row)
if conn:
conn.close()
def print_help():
""" Help Docstring"""
pass
def test():
""" Testing Docstring"""
pass
if __name__=='__main__':
# test()
sp_file = "sowpods.txt"
load_db(word_list(sp_file))
app.run(debug=True)