• python 中如何将文本中的所有单词首字母转换为大写,其余为小写


    1、

    [root@PC1 test2]# ls
    a.txt  test.py
    [root@PC1 test2]# cat a.txt       ## 测试数据
    dsfs JHDSF
    ADFD dfesr
    fKHJ FJfdf
    [root@PC1 test2]# cat test.py     ## 转换程序
    #!/usr/bin/python
    
    in_file = open("a.txt", "r")
    out_file = open("result.txt", "w")
    
    lines = in_file.readlines()
    
    for i in lines:
        temp = i.split()
        for j in range(0,len(temp)):
            temp[j] = temp[j].capitalize()
        out_file.write(" ".join(temp))
        out_file.write("\n")
    
    in_file.close()
    out_file.close()
    [root@PC1 test2]# python test.py       ## 执行程序
    [root@PC1 test2]# ls
    a.txt  result.txt  test.py
    [root@PC1 test2]# cat result.txt       ## 转换结果
    Dsfs Jhdsf
    Adfd Dfesr
    Fkhj Fjfdf

  • 相关阅读:
    js面向对象和PHP面相对象
    git
    css3动画、2D与3D效果
    渲染数据方式
    ajax
    面向对象
    Date 日期
    Math 数值对象
    What is CGLib and JDK动态代理
    IDEA中lock对象不提示newCondition();
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16336154.html
Copyright © 2020-2023  润新知