python - 使用 python 写入文本文件时,我需要更新特定列

标签 python postgresql

我正在使用 pyhton 2.7 来比较数据集。我调用 select 语句从两个表中提取数据。 str(row[4]) 始终为空白。我需要始终在该列中写一个“T”。

在我在下面的代码中使用的 g.write 语句中,有没有办法写入 str(row[4])

这是我的部分代码:

## This is used to connect to our database and run the sql statement.
def RunSQLStatement(sql):
    conn = psycopg2.connect(Connect2DB())
    cursor = conn.cursor()
    cursor.execute(sql)
    alias_data = cursor.fetchall()
    cursor.close()
    conn.close()
    return alias_data

def selectFoisp():
    with open(outTemp_file, 'w') as g:
        strGetDiffer = """SELECT c.foi_id, c.title, c.feat_type, c.feat_subtype, c.upd_type, p.foi_id, p.title
    FROM foisp c, foisp_prior p
    WHERE (c.foi_id = p.foi_id)
    AND (c.title <> p.title)"""

    extract_rows = RunSQLStatement(strGetDiffer)

    for row in extract_rows:
        g.write(str(row[0]) + ',' + str(row[1]) + ',' + str(row[2])  + ',' + str(row[3]) + ',' + str(row[4]) + ',' +  str(row[5]) + ',' + str(row[6]) + '\n')

最佳答案

g.write(str(row[0]) + ',' + str(row[1]) + ',' + str(row[2])  + ',' + str(row[3]) + ', T,' +  str(row[5]) + ',' + str(row[6]) + '\n')

或使用 format method :

g.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}\n".format(row[0], row[1], row[2], row[3], "T", row[5], row[6])

关于python - 使用 python 写入文本文件时,我需要更新特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30507699/

相关文章:

python - 用python读取二进制文件

PostgreSQL 9.1 : Find exact expression in title

sql - 在 PostgreSQL 中将整数转换为枚举

python - Scrapy、Splash和连接被对方​​拒绝: 10061

Python Socket 接收大量数据

java - 单表单元格中的对象

ruby-on-rails - ActiveAdmin 和 CircleCI : autoloader causes Postgres to fail

postgresql - 为 PostgreSQL 设置 lc_monetary

python - 在 Python 中,如何引用包含连字符的 XML 标记

python - 并行化一个让两个玩家相互对抗的 Python for 循环(博弈论模拟)