ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python: file IO

2022-03-01 13:31:48  阅读:222  来源: 互联网

标签:valent IO Python newline None valet start file valor


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

newline:  (windows)       Only for text mode

写:

  1. newline=None (default)

     

     

  2. newline=''


     

     

     

     

  3. newline='\n'

     

     

     

     

     

  4. other

     

     

     

     

     

     

     

     

     

     

     

     

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

读:

原文件

 

  1.  newline=None
  2. newline='' or '\n' or '\r' '\r\n'

     都没做转换

    牵扯到readline() readlines()

定律:

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

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

 

 

 

 

 

 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)

 

标签:valent,IO,Python,newline,None,valet,start,file,valor
来源: https://www.cnblogs.com/dissipate/p/15949767.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有