简体   繁体   中英

Merging 300 variables with Python

Five lines are correct:

GO                                 #Fixed 
EXEC sys.sp_cdc_enable_table       #Fixed
@source_schema = N'RWAW3',         #Fixed
@source_name=N'TUY1                #Variable 
@role_name = NTEDULL               #Fixed

I have 300 codes, 100 of which are missing some fixed lines My program recognizes lines that contain variable values, and I want to combine them with fixed lines 300 times

mylines = []
fname = input("Enter file name: ")
with open (fname) as myfile:
    for myline in myfile:
        mylines.append(myline)
l1 = mylines[0]
l2 = mylines[1]
l3 = mylines[2]
l4 = mylines[3]
l5 = mylines[4]
tl1 = mylines.count(l1)
tl2 = mylines.count(l2)
tl3 = mylines.count(l3)
tl4 = mylines.count(l4)
tl5 = mylines.count(l5)
print(l1.strip () ,  tl1)
print(l2.strip () ,  tl2)
print(l3.strip () ,  tl3)
print(l4.strip () ,  tl4)
print(l5.strip () ,  tl5)
al = len(mylines) 
t = [tl1,tl2,tl3,tl4,tl5]
t.remove(1)
cn= min(t) 
cocm = max(t) 
cncm = cocm - cn 
tl = cn * 5
print('\nMissing lines:' , cncm)
print('Salting, Go to code:' , tl)

lst = list()
for line in fh:
    word= line.rstrip().split()
    for element in word:
        if element in lst:
            continue
        else :
            lst.append(element)

Required 1-:

cocm=312
print(l1\n,l2\n,l3\n,lst[0}\n,l5)

Repeated 312 (cocm) times As the value in the lst() changes in ascending order

Required 2-:

tl4 = mylines.count(l4)
print(tl4)
>>> 1

The program recognized the line l4 as a variable so it added in the lst() menu I want to know the position when printing

I got an answer

mylines = []
mmm=[]
fname = input("Enter file name: ")
#with open (fname) as myfile:
myfile =  open (fname)
for myline in myfile:
        mylines.append(myline)
        if myline in mmm :
             continue
        else:
             mmm.append(myline)
mm=mmm[5:]
l1 = mylines[0]
l2 = mylines[1]
l3 = mylines[2]
l4 = mylines[3]
l5 = mylines[4]
tl1 = mylines.count(l1)
tl2 = mylines.count(l2)
tl3 = mylines.count(l3)
tl4 = mylines.count(l4)
tl5 = mylines.count(l5)
print(l1,l2,l3,l4,l5.strip ())
x = len(mm)
while x > 0 :
    if tl1 == 1  : print(mm[0].strip ())
    else : print(l1.strip ())
    if tl2 == 1 : print(mm[0].strip ())
    else : print(l2.strip ())
    if tl3 == 1 : print(mm[0].strip ())
    else : print(l3.strip ())
    if tl4 == 1 : print(mm[0].strip ())
    else: print(l4.strip ())
    if tl5 == 1 : print(mm[0].strip ())
    else : print(l5.strip ())
    del mm[0]
    x = x-1
else:
 quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM