Python 如果单词匹配,则仅替换第n个字段
所以我有一个代码,我想这样做: 如果UniqueID from file1=UniqueID from file2: 替换UniqueID文件1的第n个字段 示例数据: 姓名: 太呜呜呜呜苏卓Python 如果单词匹配,则仅替换第n个字段,python,python-2.7,if-statement,for-loop,text,Python,Python 2.7,If Statement,For Loop,Text,所以我有一个代码,我想这样做: 如果UniqueID from file1=UniqueID from file2: 替换UniqueID文件1的第n个字段 示例数据: 姓名: 太呜呜呜呜苏卓 infile = path filename = "Names.txt" otherin = path othfile = "OtherNames.txt" otherpth = os.path.join(otherin,othfile) path = os.path.join(infile,file
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
Poo Boo Moo Moo Soo Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
古宝露木素秀
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
OooBoo-Poo-Moo-Soo-Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
其他名称:
IIo Boo Moo Moo Soo Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
太呜呜呜呜呜呜呜呜呜呜
OooBoo-YOO-Moo-Soo-Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
最终结果:
太Boo NEW Moo Soo Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
Poo Boo Moo Moo Soo Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
古宝露木素秀
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
OooBoo NEW Moo Soo Choo
infile = path
filename = "Names.txt"
otherin = path
othfile = "OtherNames.txt"
otherpth = os.path.join(otherin,othfile)
path = os.path.join(infile,filename)
mop = open(otherpth,"r")
rdmo = mop.readlines()
L = list(rdmo)
doc = open(path,"r")
doc2 = doc.readlines()
k = list(doc2)
for words in k:
wordsplit = words.split(" ")
first = wordsplit[0]
sec = wordsplit[1]
third = wordsplit[2]
fourth = wordsplit[3]
fifth = wordsplit[4]
sixth = wordsplit[5]
allwords = first +" "+ sec +" "+ third +" "+ fourth +" "+ fifth+" "+sixth
for entry in L:
entsplit = entry.split(" ")
a = entsplit[0]
b = entsplit[1]
c = entsplit[2]
d = entsplit[3]
e = entsplit[4]
f = entsplit[5].replace("\n","")
mtch = a +" "+ b+" "+ c+" "+ d+" "+ e+" "+f
if first == a:
newword = allwords[:8]+"NEW "+allwords[12:]
final = path
text = "NamesFinal.txt"
finaltext = os.path.join(final,text)
docfinal = open(finaltext,"w")
for lines in doc2:
G = lines.replace(allwords,newword)
docfinal.write(G)
目前,它只是吐出Ooo的最后一个循环。当再次出现时,我希望它吐出文本文件的名称,除非这些字段更改为New。匹配的每一行在第三列中都有一个“New”
import csv
with open('path/to/names') as infile:
db1 = {}
for row in csv.reader(infile, delimiter=' '):
db1[row[0]] = row
with open('path/to/otherNames') as infile:
db2 = set()
for row in csv.reader(infile, delimiter=' '):
db2.add(row[0])
for k in db1:
if k in db2:
db1[k][2] = "NEW"
with open('path/to/names') as infile, open('path/to/output', 'w') as outfile:
writer = csv.writer(outfile, delimiter=' ')
for line in csv.reader(infile, delimiter=' '):
k = line[0]
writer.writerow(db1[k])
你确定你不想在输出中出现“Ooo Boo YOO Moo Soo Choo”吗?不想。我只想用新的替换第三项。抱歉,如果这是混淆,因为其中有新的。不,不,YOO,抱歉混淆。我想要新的。@Josh Clayton:好了!匹配的所有内容现在都将替换为“新”