使用mysqlcursor覆盖第一个字符的Python打印函数

使用mysqlcursor覆盖第一个字符的Python打印函数,python,mysql,command-prompt,Python,Mysql,Command Prompt,在从数据库打印时,我遇到了一个奇怪的错误,不确定问题是由于python解码还是其他原因造成的。我尝试使用sys.stdout.write('.'),它有类似的输出问题 *更新1 Powershell具有相同的输出,而不同的窗口大小没有任何影响 *更新2 找到解决方案:添加field.decode().strip()而不是field.decode()删除了'/r'字符 谢谢你@tdelaney 代码 数据库表 所有字段都是varchar 天气记录 thead tr{背景色:ActiveCapt

在从数据库打印时,我遇到了一个奇怪的错误,不确定问题是由于python解码还是其他原因造成的。我尝试使用sys.stdout.write('.'),它有类似的输出问题

*更新1 Powershell具有相同的输出,而不同的窗口大小没有任何影响

*更新2 找到解决方案:添加field.decode().strip()而不是field.decode()删除了'/r'字符

谢谢你@tdelaney

代码 数据库表 所有字段都是varchar


天气记录
thead tr{背景色:ActiveCaption;颜色:CaptionText;}
th,td{垂直对齐:顶部;字体系列:“Tahoma”,Arial,Helvetica,无衬线;字体大小:10pt;填充:3px;}
表,td{边框:1px纯银;}
表{边框折叠:折叠;}
thead.col0{宽度:101px;}
thead.col1{宽度:82px;}
thead.col2{宽度:85px;}
thead.col3{宽度:67px;}
thead.col4{宽度:85px;}
thead.col5{宽度:78px;}
thead.col6{宽度:122px;}
thead.col7{宽度:142px;}
thead.col8{宽度:135px;}
thead.col9{宽度:86px;}
thead.col10{宽度:102px;}
thead.col11{宽度:118px;}
thead.col12{宽度:98px;}
thead.col13{宽度:118px;}
thead.col14{宽度:119px;}
thead.col15{宽度:86px;}
thead.col16{宽度:102px;}
thead.col17{宽度:118px;}
thead.col18{宽度:98px;}
thead.col19{宽度:118px;}
thead.col20{宽度:119px;}
日期
明特姆
maxTemp
降雨量
蒸发
阳光
maxWindGustDir
最大风速
最大风速时间
9月9日
9湿度
9amCloudCover
9amWindDir
9Mwindspeed
9主要压力
3pmTemp
3pmh湿度
3pmCloudCover
3pmWindDir
3Mwindspeed
3主压力
2017-02-28
10.9
33.3
0
N
30
09:24
21.2
66
东南方
7.
1022.3
32.1
22
NNE
9
1019.9

在标准端子中,
\r
字符将插入点移回当前行的起始位置。如果你的台词前面被覆盖,很有可能是有罪的一方


根本原因是将数据放入数据库时未正确清理。如果无法清理数据库本身,解决方案是每次使用时都要清理它。

您确定这不是命令提示符包装吗?尝试在PowerShell中运行脚本,并使窗口变大。PowerShell具有相同的输出。可能里面有一个像a\r这样的奇怪字符。对于debug,将其更改为
repr(field.decode())
-在那里会有额外的引号,但会看到奇数字符。
1019.9
wrapps。所以,
field.decode().strip()
可以修复它。顺便说一句,我认为您不需要额外的
str(…)
。谢谢您解决了这个问题,是的,1019.9似乎有一个\r字符
import mysql.connector
import sys
import os
import sys
from datetime import datetime

#################################

# Database settings

hostname = "localhost"
username = "root"
password = "---------"
database = "weatherdb"
port = 3306

# arguments
csvFile = ""
tableName =""

#################################

# Simple routine to run a query on a database and print the results:
def doQuery(conn):
    cur = conn.cursor()

    cur.execute( "SELECT * FROM weather_record" )
    count = 0
    for row in cur:
        res = ""
        for field in row:
            res = res + str(field.decode())+ "|"  # line in question
            print(res)               
        res = res + "\n"

#################################
# Get user arguments
def main(argv):
    if(len(sys.argv)== 3):
        # correct number of arguments
        pw = sys.argv[2]
        csvFile = sys.argv[1]
        loadToDB()
    else:
        # incorrect number of arguments
        currentFileName = os.path.basename(__file__)
        print(f"usage {currentFileName} inputfile.csv tableName")
        sys.exit(2)


#################################

print("Using mysql.connector…")
myConnection = mysql.connector.connect(host=hostname, user=username, port=port, passwd=password, db=database)
doQuery(myConnection)
myConnection.close()

#################################

# Run main function on script start
if __name__ == "__main__":
    main(sys.argv[1:])