我有一个变量 (aTerm),其中包含一个 8 位随机数,我用它来唯一标识 csv 文件的每一行。
这是我编写的用于删除具有相应编号的行的代码:
def rebuildFile(aTerm, aFile):
with open(aFile, 'r') as oldFile, open('static\\new.csv', 'w') as newFile:
for line in oldFile:
if not aTerm in line:
newFile.write(line)
return
唯一的问题是,除非我在 if 语句中以字符串形式输入 8 位数字,否则它根本不起作用。所以这很有效:
def rebuildFile(aTerm, aFile):
with open(aFile, 'r') as oldFile, open('static\\new.csv', 'w') as newFile:
for line in oldFile:
if not "45893243" in line:
newFile.write(line)
return
我不明白,非常感谢您的帮助,谢谢
最佳答案
您需要将 aTerm
转换为 str
以便将其与 str
进行比较:
def rebuildFile(aTerm, aFile):
sTerm = str(aTerm)
with open(aFile, 'r') as oldFile, open('static\\new.csv', 'w') as newFile:
for line in oldFile:
if not sTerm in line:
newFile.write(line)
关于python - 如何检查 csv 文件行中是否存在等于变量的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47646112/