python 读写文件格式化输出

Python读写文件
1.open
file_object = open('thefile.txt')
try:
     all_the_text = file_object.read( )
finally:
     file_object.close( )

2.读文件
读文本文件
input = open('data', 'r')
#第二个参数默认为r
input = open('data')

读二进制文件
input = open('data', 'rb')

读取所有内容
file_object = open('thefile.txt')
try:
     all_the_text = file_object.read( )
finally:
     file_object.close( )


读固定字节
file_object = open('abinfile', 'rb')
try:
    while True:
         chunk = file_object.read(100)
        if not chunk:
            break
         do_something_with(chunk)
finally:
     file_object.close( )


读每行
list_of_all_the_lines = file_object.readlines( )

如果文件是文本文件,还可以直接遍历文件对象获取每行:

for line in file_object:
     process line


3.写文件
写文本文件
output = open('data', 'w')


写二进制文件
output = open('data', 'wb')


追加写文件
output = open('data', 'w+')


写数据
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )


写入多行
file_object.writelines(list_of_text_strings)

注意,调用writelines写入多行在性能上会比使用write一次性写入要高。

示例:

from Finder import process

import QiweiLogger

file_object = open('/work/not_dispatch.txt')
file_writer_object = open('/work/result.txt', 'w+')

for line in file_object:
    # print line
    var1 = line.split(',')
    # var2 = var1[1].split(',')
    # print var1
    crawlSite = '';
    firstLevelChannel = '';
    secondLevelChannel = '';
    subSite = '';
    for item in var1:
        # print item
        if item.strip().startswith('crawlSite'):
            print item
            crawlSite = item;
        elif item.strip().startswith('firstLevelChannel'):
            # print item
            firstLevelChannel=item;
        elif item.strip().startswith('secondLevelChannel'):
            # print(item)
            secondLevelChannel=item;
        elif item.strip().startswith('subSite'):
            # print item
            subSite = item;

            # print('%s %s %s %s' % (crawlSite, firstLevelChannel, secondLevelChannel, subSite))
            resultStr = '%s %s %s %s' % (crawlSite, firstLevelChannel, secondLevelChannel, subSite)+'\n'
            print resultStr

            file_writer_object.writelines(resultStr)

file_object.close()
file_writer_object.close()
            # print("EPG value changed to %s" % (value))
        # else:
        #     print 'not'
        # qiweiLogger = QiweiLogger(crawlSite, firstLevelChannel, secondLevelChannel, subSite)
        # qiweiLogger = QiweiLogger('handetian', '18210413001', '[email protected]', '123456')
        # print qiweiLogger.get_crawlSite()


猜你喜欢

转载自junege.iteye.com/blog/2366612