如何在python3.x中写入文本文件?

How to write text file in python3.x?

我无法写入文本文件。我的功能看起来像这样。文件已创建,但在 运行 代码之后文件仍为空。我无法弄清楚问题是什么。

def compare(prjCode, prjName, stCode, stName, dCode, dName, sdCode, sdName):
    with open('C:\Users\NCOG1\Desktop\test.txt', 'w') as f:
        percentMatch = 0
        sdLen = len(sdName)
        prjLen = len(prjName) 
        if (sdLen > prjLen): 
            if ((sdName.find(prjName) != -1)):
                res = sdName.index(prjName)
                percentMatch = (prjLen/sdLen)*100
                #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
                #print(prjCode + "," + prjName + "," + sdCode + "," + sdName + str(round(percentMatch)) + "," + stCode + "," + dCode + "," + sdCode)
                f.write(prjCode + "," + prjName + "," + sdCode + "," + sdName + "," + str(round(percentMatch)) + "," + stCode + "," + stName + "," + dCode + "," + dName + "%\n")
            else:
                res = 0
                #print(res)
        elif (prjLen >= sdLen):
            if ((prjName.find(sdName) != -1)):
                res = prjName.index(sdName)
                percentMatch = (sdLen/prjLen)*100
                #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
                #print(prjCode + "," + prjCode + "," + sdCode + "," + sdName + str(round(percentMatch) + "," stCode + "," + dCode + "," + sdCode)
                f.write(prjCode + "," + prjName + "," + sdCode + "," + sdName + "," + str(round(percentMatch)) + "," + stCode + "," + stName + "," + dCode + "," + dName + "%\n")                                                    
            else:
                res = 0
                #print(res)  
    f.close()

1.For 要在文件中写入内容,您需要将那些文本或 int 转换为字符串,而您没有这样做,这是主要问题

2.The 您键入的路径未呈现,因为它未被识别为字符串,您需要将路径转换为原始字符串,因此您需要在开始之前附加 "r"我在更正代码中提到的路径。

3.For 示例我为虚拟数据调用函数 compare(),现在检查下面的代码并尝试 运行 如果您有任何疑问请告诉我。

def compare(prjCode, prjName, stCode, stName, dCode, dName, sdCode, sdName):
    with open(r'C:\Users\NCOG1\Desktop\test.txt', 'w') as f:
    percentMatch = 0
    sdLen = len(sdName)
    prjLen = len(prjName) 
    if (sdLen > prjLen): 
        if ((sdName.find(prjName) != -1)):
            res = sdName.index(prjName)
            percentMatch = (prjLen/sdLen)*100
            #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
            #print(prjCode + "," + prjName + "," + sdCode + "," + sdName + str(round(percentMatch)) + "," + stCode + "," + dCode + "," + sdCode)
            f.write(str(prjCode) + "," + prjName + "," + str(sdCode) + "," + sdName + "," + str(round(percentMatch)) + "," + str(stCode) + "," + stName + "," + str(dCode) + "," + str(dName) + "%\n")
        else:
            res = 0
            #print(res)
    elif (prjLen >= sdLen):
        if ((prjName.find(sdName) != -1)):
            res = prjName.index(sdName)
            percentMatch = (sdLen/prjLen)*100
            #print(prjCode,prjName,sdCode,sdName,str(round(percentMatch)),stCode, stName, dCode, dName)
            #print(prjCode + "," + prjCode + "," + sdCode + "," + sdName + str(round(percentMatch) + "," stCode + "," + dCode + "," + sdCode)
            f.write(str(prjCode) + "," + prjName + "," + str(sdCode) + "," + sdName + "," + str(round(percentMatch)) + "," + str(stCode) + "," + stName + "," + str(dCode) + "," + str(dName) + "%\n")                                                    
        else:
            res = 0
            #print(res)  


compare(1,"harsh",0,"fff",34,32,78,"hharshbhut")