-1

私が書いた以下のコードは、名と姓を含むサンプル ファイルから入力を取得します。次に、それらの名前をサンプル メールに変換します。何らかの理由で、スクリプトは同じ姓を何度も出力し続けます。

namess.txt は次のようになります: 名、姓

コード:

import os, re, time, getpass, linecache

Original = os.path.join(os.path.expanduser('~'), 'Desktop','namess.txt')
File = os.path.join(os.path.expanduser('~'), 'Desktop','output.txt')
badNames = []
Names = []

def RemCommas():
    outfile = open(os.path.join('C:\\', 'Users', getpass.getuser(), 'Desktop','output.txt'),'w')
    Filedata = open(Original).read()
    outfile.write(re.sub(',', ' ', Filedata))
    outfile.close()

def ClassNum():
    count = 6
    Year = int(time.strftime('%Y'))
    Class = str((Year - 2013) + 6)
    return Class

def ReadStoreFile():
    i = 0
    OpenFile = open(File) 
    LenFile = len(OpenFile.readlines())
    while i < LenFile:
        i += 1
        badNames.append(linecache.getline(File, i))

def CleanNames():
    i = 0
    while i < len(badNames):
        cleaned = badNames[i].rstrip()
        Names.append(cleaned)
        i += 1

def NamePrint():
    Interns = 'makchessclub.org'
    arrayname = []
    i = 0
    j = 0
    m = 0

    while m < len(Names): 
        Name = Names[m]
        Name = Name.lower()
        InternName = Name[0] + Name[1]
        #------------Checking for space and first name--
        while i < len(Name):
            if Name[i] == ' ':
                i = Name.index(' ')
                break;   
            i += 1
        #---------------adding last name in an array----
        Namelen = len(Name) - (i+1)
        while j < Namelen:
            arrayname.append(Name[i+1])
            j += 1
            i += 1
        #---------------Final Name Print----------------
        Lastname = ''.join(arrayname)
        #print arrayname
        #Lastname = Lastname.strip(' ')
        #print InternName + Lastname + ClassNum() + Interns
        file = open('C:\\Users\\username\\Desktop\\emails.txt', 'a')
        file.write(InternName + Lastname + ClassNum() + Interns + '\n')
        file.close()
        m += 1

RemCommas()
ReadStoreFile()
CleanNames()
NamePrint()

print ''
os.system('pause')    
4

2 に答える 2