-1

我编写的下面的代码从包含名字和姓氏的示例文件中获取输入。然后它将这些名称转换为示例电子邮件。出于某种原因,脚本会一遍又一遍地打印相同的姓氏。

names.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

2

姓氏不变的原因是您没有arrayname在循环中重置。你不断地给它附加名字,程序会选择第一个。所以你应该把你arrayname = []while m < len(Names):

于 2013-05-26T01:21:22.590 回答
2

我猜这就是你想要做的:

import os
import re
import time

def create_mails(input_path, output_path, year, addr):
    with open(input_path, 'r') as data:
        mail = re.sub(r'(\w+)\s*,\s*(\w+)\n?', r'\1\g<2>%s%s\n' % (year, addr), data.read())
    with open(output_path, 'w') as output:
        output.write(mail.lower())
    print 'Mail addresses generated and saved to', output_path

演示:

create_mails(
    os.path.join(os.path.expanduser('~'), 'Desktop', 'namess.txt'),
    os.path.join(os.path.expanduser('~'), 'Desktop', 'output.txt'),
    str(int(time.strftime('%Y')) - 2013 + 6),
    '@makchessclub.org'
)

如果namess.txt是这样的:

First, Last
John,Doe
Spam, Ham
Cabbage, egg

然后output.txt会是这样的:

firstlast6@makchessclub.org
johndoe6@makchessclub.org
spamham6@makchessclub.org
cabbageegg6@makchessclub.org
于 2013-05-26T01:24:53.883 回答