使用python处理文件,将文件中的每一行首字母大写
2017-07-13 00:00
483 查看
摘要: 持续学习中,做个简单记录……
大写:ctrl+shift+u
小写:ctrl+u
一、背景:
有个文本文件,内容不多,想将每行数据的首字母大写。$ cat test map filter flatMap mapPartitions mapPartitionsWithIndex sample union intersection distinct groupByKey reduceByKey aggregateByKey sortByKey join cogroup cartesian pipe coalesce repartition repartitionAndSortWithinPartitions
二、处理办法
1.使用python处理,目前想到两种处理办法:
$ cat dlw.py
#!/usr/bin/python # -*- coding: UTF-8 -*- class Capfile: def __init__(self, f): self.f = f # 方法1,使用python函数capitalize()来处理,这个处理的结果数据会变成首字母大写,其它字母都小写了 def capfile1(self): of = open(file1) wf = open(file2, 'w') for i in of.readlines(): d = i.capitalize() print d wf.write(d) of.close() wf.close() # 方法2,使用upper对第一个字母进行大写转换,然后拼接剩下的字母 def capfile2(self): of = open(file1) wf = open(file3, 'w') for i in of.readlines(): d = i[0].upper() + i[1:] print d wf.write(d) of.close() wf.close() file1 = "/home/spark/test" file2 = "/home/spark/test1" file3 = "/home/spark/test2" f = "" inst = Capfile(f) inst.capfile1() inst.capfile2()
2.使用软件处理
使用Notepad++,列编辑模式选中首行大写:ctrl+shift+u
小写:ctrl+u
相关文章推荐
- Python 处理 ini 格式文件 | ConfigParser的使用 [Python俱乐部]
- 使用python递归子目录处理日志文件
- 使用Python处理地理数据文件-多进程处理类
- Python-使用CSV模块进行文件处理
- 基因数据:使用Python处理vcf文件
- 使用python处理文件
- 详解使用Python处理文件目录的相关方法
- 使用python处理中文csv文件,并让excel正确显示中文(避免乱码)
- Python下使用help(dict),显示'more'不是内部或外部命令,也不是可运行的程序或批处理文件,该如何处理?
- 使用python处理中文csv文件,并让excel正确显示中文
- 使用python处理文件
- 如何使用shell将文件中单词首字母转换为大写
- 使用Python递归对文件进行相关处理
- Python open()文件处理使用介绍
- 学习python的第四十五天-第七章 使用python处理文件
- 用python处理文本,本地文件系统以及使用数据库的知识基础
- Python笔记–IAR使用Python脚本实现自动对烧录文件进行处理
- 使用jQuery 把一个JSP文件中的所有id名字替换成以b开头,再加上原来的名字(首字母变为大写)
- Python使用pandas处理CSV文件
- 使用python处理csv文件