gpt4 book ai didi

python - 将数据从表导出到文本文件时出现意外类型错误

转载 作者:行者123 更新时间:2023-11-29 14:31:44 25 4
gpt4 key购买 nike

我正在尝试将表中的数据导出到 txt 文件。我有如下代码

class Optimization_goolge(object):
def __init__(self):
self.dbname = ""
self.usr_name = ""
self.usr_pass = ""
self.con = ""
self.cur = ""
self.openTime=""

def vT(self, x, y, z, openTime):
try:
con_string="dbname = '%s' user = '%s' password ='%s' host = 'localhost'"%(x,y,z)
con = psycopg2.connect(con_string)
con.set_isolation_level(0)
cur = con.cursor()

cur.execute(("""COPY (select source, target, sum(cost)/1000 as cost from distance_matrix where source != 88888888 and target
!= 88888888 group by source, target order by source) TO '%s\\vrp_distance.txt'""") % (os.getcwd()))
con.commit()

但是我得到了错误

error vT Function could not open file "D:\working_copy\vrp_distance.txt" for writing: No such file or directory

Error optimizeFxn_google arguments did not match any overloaded call:
QMessageBox.warning(QWidget, QString, QString, QMessageBox.StandardButtons buttons=QMessageBox.Ok, QMessageBox.StandardButton defaultButton=QMessageBox.NoButton): argument 1 has unexpected type 'Optimization_goolge'
QMessageBox.warning(QWidget, QString, QString, int, int, int button2=0): argument 1 has unexpected type 'Optimization_goolge'
QMessageBox.warning(QWidget, QString, QString, QString, QString button1Text=QString(), QString button2Text=QString(), int defaultButtonNumber=0, int escapeButtonNumber=-1): argument 1 has unexpected type 'Optimization_goolge'

最佳答案

尝试:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, 'vrp_distance.txt')

sql_statement = ("COPY (select source, target, sum(cost)/1000"
" as cost from distance_matrix where source != 88888888 and target"
" != 88888888 group by source, target order by source) TO"
" '{}'".format(filepath))

要考虑的旁注(不在原始问题中):

  • 为什么不分离提取信息并将其写入文件的职责——你会知道哪一部分失败了
  • OptimizationGoolge,按照惯例
  • 如果你用空字符串初始化一个类,类设计就会有问题
  • vT 重命名为有意义的名称

你也必须检查:

assert os.path.exists('D:/working_copy/vrp_distance.txt') 
assert os.path.exists('D:/working_copy')

同时检查 Save PL/pgSQL output from PostgreSQL to a CSV file .如果您没有书面许可,以上所有都是徒劳的。

关于python - 将数据从表导出到文本文件时出现意外类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com