Python 将输出写入文件不工作:未找到错误
我试图用Python在一个文件中编写一个输出结果,但它不起作用,也没有显示任何错误 以下是我的代码:Python 将输出写入文件不工作:未找到错误,python,Python,我试图用Python在一个文件中编写一个输出结果,但它不起作用,也没有显示任何错误 以下是我的代码: from collections import namedtuple from pprint import pprint as pp final_data = [] inf = float('inf') Edge = namedtuple('Edge', 'start, end, cost') class Graph(): def __init__(self, edges): self
from collections import namedtuple
from pprint import pprint as pp
final_data = []
inf = float('inf')
Edge = namedtuple('Edge', 'start, end, cost')
class Graph():
def __init__(self, edges):
self.edges = edges2 = [Edge(*edge) for edge in edges]
self.vertices = set(sum(([e.start, e.end] for e in edges2), []))
def dijkstra(self, source, dest):
assert source in self.vertices
dist = {vertex: inf for vertex in self.vertices}
previous = {vertex: None for vertex in self.vertices}
dist[source] = 0
q = self.vertices.copy()
neighbours = {vertex: set() for vertex in self.vertices}
for start, end, cost in self.edges:
neighbours[start].add((end, cost))
#pp(neighbours)
while q:
u = min(q, key=lambda vertex: dist[vertex])
q.remove(u)
if dist[u] == inf or u == dest:
break
for v, cost in neighbours[u]:
alt = dist[u] + cost
if alt < dist[v]:
dist[v] = alt
previous[v] = u
#pp(previous)
s, u = [], dest
while previous[u]:
s.insert(0, u)
u = previous[u]
s.insert(0, u)
return s
start_point = input('Enter the starting point: ')
end_point = input('Enter the ending point: ')
file_name = input('Enter the input file name: ')
output_file = input('Enter the output file name: ')
f = open(file_name, 'r')
data = f.readlines()
for line in data:
f_line = line
values = f_line.split()
values[2] = int(values[2])
final_data.append(values)
graph = Graph(final_data)
f = open(output_file, 'a')
result = str(pp(graph.dijkstra(start_point, end_point)))
f.write(result)
从集合导入namedtuple
从pprint导入pprint作为pp
最终数据=[]
inf=浮点('inf')
Edge=namedtuple('Edge','start,end,cost'))
类图():
定义初始(自,边):
self.edges=edges2=[Edge(*Edge)表示边中的边]
self.vertices=set(总和([e.start,e.end]表示边2中的e),[]))
def dijkstra(自身、源、目标):
在self.vertices中断言源
dist={vertex:inf表示self.vertexs中的顶点}
previous={vertex:self.vertexs}中的顶点无
距离[源]=0
q=self.vertices.copy()
邻居={vertex:set()表示self.vertexs}中的顶点
对于开始、结束、成本(以自我为单位):
邻居[开始]。添加((结束,成本))
#pp(邻居)
而q:
u=min(q,key=lambda顶点:距离[顶点])
q、 移除(u)
如果dist[u]==inf或u==dest:
打破
对于v,邻近成本[u]:
alt=距离[u]+成本
如果alt
注意:忽略缩进
我不确定我到底在哪里犯了错误。我尝试使用append(a)和write(w)写入文件,但两者都不起作用。文件的所有路径都正确,输入文件读取效果良好,输出文件也在同一文件夹中
f = open(output_file, 'a')
result = str(pp(graph.dijkstra(start_point, end_point)))
f.write(result)
相反,您希望:
with open(output_file, 'a') as f:
result = pprint.pformat(graph.dijkstra(start_point, end_point))
f.write(result)
因为pprint.pprint()pprint.pformat
已生成,因此您可以“打印”到字符串。不过,您可能想这样做
with open(output_file, 'a') as f:
pp(graph.dijkstra(start_point, end_point), stream=f)
如pprint的帮助所述:
Help on function pprint in module pprint:
pprint(object, stream=None, indent=1, width=80, depth=None)
Pretty-print a Python object to a stream [default is sys.stdout].
你最后忘记了f.close()
。谢谢你的帮助,但现在它写的是None而不是确切的答案,这是因为pprint.pprint
返回None
。你想要pprint.pformat()
非常感谢-它成功了。祝你今天愉快