lower()函数不带参数

时间:2017-03-22 21:34:09

标签: python python-3.x lowercase

def main():

    print("this program creates a file of usernames from a ")
    print("files of names ")
    # get the file names

    infilename = input("what files are the name in")
    outfilename = input("what file should the usernames go in")
    # open the files

    infile = open(infilename,'r')
    outfile = open(outfilename,'w')
    # process each line of the input file
    for line in infile.readlines():
        # get the first and last names from line
        first, last = line.split()
        # create the username
        uname = line.lower(first[0]+last[:7])
        # write it to the output file
        outfile.write(uname+'\n')
    # close both files
    infile.close()
    outfile.close()

    print("usernames have been written to : ", outfilename)

main()

我正在尝试编写一个程序,该程序从文件中获取一堆名字和姓氏,然后打印一个用户名,该用户名是第一个名字的第一个字母和姓氏的组合。示例:alex doug将为adoug

python解释器在uname = line.lower(first[0]+last[:7])上显示错误。

TypeError lower() takes no arguments (1 given)

有没有解决此错误的方法,还是有其他方法可以做到这一点?

2 个答案:

答案 0 :(得分:2)

正确书写,相关的行可能如下:

uname = (first[0]+last[:7]).lower()

...或者,更详细地说:

uname_unknown_case = first[0]+last[:7]
uname = uname_unknown_case.lower()

值得注意的是,用作输入的字符串是调用该方法的对象;正如错误信息所说,没有其他论据。

答案 1 :(得分:0)

较低的功能不起作用。如果要在python中将文本转换为小写,则必须执行以下操作:

string1 = "ABCDEFG"
string2 = string1.lower()
print(string2) # prints abcdefg