Python: file IO

Python: file IO

 

 Python: file IO

 Python: file IO

 

 

Python: file IO

 

 Python: file IO

 

 Python: file IO

 

 Python: file IO

 

 

Python: file IO

 

 

 Python: file IO

 

 

newline:  (windows)       Only for text mode

写:

  1. newline=None (default)
    Python: file IO

    Python: file IO

     

     

  2. newline=''


    Python: file IO

     

     Python: file IO

     

     

  3. newline='\n'

     

     Python: file IO

     

     Python: file IO

     

  4. other
    Python: file IO

     

     Python: file IO

     

     Python: file IO

    Python: file IO

     

     

     Python: file IO

     

     

    Python: file IO

     

     Python: file IO

     

     
    Summarize: 写文件时, 全部使用 '\n' , newline=None 替换成 os.linesep,     newline='' or '\n' 不做替换, newline='\r' or '\r\n' 替换成 '\r' or '\r\n'

读:

原文件

Python: file IO

 

  1.  newline=None
    Python: file IO
  2. newline='' or '\n' or '\r' '\r\n'
    Python: file IO

     都没做转换

    牵扯到readline() readlines()

定律:

读文件采用默认 newline=None, Python把 \r \n \r\n 都自动转换为 \n, 进行处理

写文件采用 换行统一使用 \n, newline=None时, 写道文件中的是 os.linesep,  newline='\n' or '', 写道文件的是 \n

Python: file IO

 

 Python: file IO

 

 Python: file IO

 

 word census 单词统计:

import string


def census(file: str, encoding = 'utf-8'):
    valet = dict()
    with open(file = file, mode = 'r+t', encoding = encoding, errors = 'strict', newline = None) as f:
        for line in f:
            valor = line.split()
            for k, v in zip(valor, (1,) * len(valor)):
                k = k.strip(string.punctuation).lower()
                valet[k] = valet.get(k, 0) + 1

    valent = sorted(valet.items(), key = lambda item: item[1], reverse = True)

    for b in range(10):
        print(str(valent[b]).strip("'()").replace("'", '').replace(',', ':'))

    return valent


statistic = census('word.txt')

# print(statistic)
import string


def census(file, encoding = 'utf-8'):
    valet = dict()
    with open(file = file, encoding = encoding, errors = 'strict', newline = None) as f:
        for line in f:
            valor = line.split()
            for k, v in zip(valor, (1,) * len(valor)):
                k = k.strip(string.punctuation).lower()  # 排除了开头和结尾的特殊符号
                # k = k.strip().lower()
                if not k:  # '' Empty String
                    continue

                start = 0

                for i, v in enumerate(k):
                    if v in set(string.punctuation):  # @abc abc@@def lib/posixpath.py valor.
                        if start == i:  # @abc abc@@d  开头 or 连续
                            start += 1
                            continue
                        valet[k[start:i]] = valet.get(k[start:i], 0) + 1
                        start = i + 1
                else:
                    if k[start:]:  # valor.. not need, k已经strip(string.punctuation)
                        # print(k[start:], k, start, 55)
                        valet[k[start:]] = valet.get(k[start:], 0) + 1

    valent = sorted(valet.items(), key = lambda item: item[1], reverse = True)
    for b in range(10):
        if b < len(valent):
            print(str(valent[b]).strip("'()").replace("'", "").replace(',', ':'))
    return valent


statistic = census('word.txt')
# print(statistic)

 

上一篇:python3 运行行报错 AttributeError: module ‘random‘ has no attribute ‘randint‘


下一篇:javaCV开发详解之12:视频转apng动态图片实现,支持透明通道,也支持摄像机、桌面屏幕、流媒体等视频源转apng动态图