首页 > 编程语言 >无涯教程-Python - 处理非结构化数据

无涯教程-Python - 处理非结构化数据

时间:2023-12-01 12:32:12浏览次数:33  
标签:教程 Python programming 无涯 large ln Line

以行和列格式存在的数据,或可以轻松转换为行和列的数据,以便以后可以很好地适合数据库的数据称为结构化数据,例如CSV,TXT,XLS文件等。

读取数据

在下面的示例中,无涯教程获取一个文本文件并读取该文件,其中分离了其中的每一行,接下来,可以将输出分为更多的行和单词。

filename = 'path\input.txt'  

with open(filename) as fn:  

# Read each line
   ln = fn.readline()

# Keep count of lines
   lncnt = 1
   while ln:
       print("Line {}: {}".format(lncnt, ln.strip()))
       ln = fn.readline()
       lncnt += 1

当执行上面的代码时,它将产生以下输出。

Line 1: Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.
Line 2: Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large and comprehensive standard library.
Line 3: Python interpreters are available for many operating systems. CPython, the reference implementation of Python, is open source software and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.

计数词频

可以使用计数器功能如下计算文件中单词的频率。

from collections import Counter

with open(r'pathinput2.txt') as f:
               p = Counter(f.read().split())
               print(p)

当无涯教程执行上面的代码时,它将产生以下输出。

Counter({'and': 3, 'Python': 3, 'that': 2, 'a': 2, 'programming': 2, 'code': 1, '1991,': 1, 'is': 1, 'programming.': 1, 'dynamic': 1, 'an': 1, 'design': 1, 'in': 1, 'high-level': 1, 'management.': 1, 'features': 1, 'readability,': 1, 'van': 1, 'both': 1, 'for': 1, 'Rossum': 1, 'system': 1, 'provides': 1, 'memory': 1, 'has': 1, 'type': 1, 'enable': 1, 'Created': 1, 'philosophy': 1, 'constructs': 1, 'emphasizes': 1, 'general-purpose': 1, 'notably': 1, 'released': 1, 'significant': 1, 'Guido': 1, 'using': 1, 'interpreted': 1, 'by': 1, 'on': 1, 'language': 1, 'whitespace.': 1, 'clear': 1, 'It': 1, 'large': 1, 'small': 1, 'automatic': 1, 'scales.': 1, 'first': 1})

参考链接

https://www.learnfk.com/python-data-science/python-processing-unstructured-data.html

标签:教程,Python,programming,无涯,large,ln,Line
From: https://blog.51cto.com/u_14033984/8644382

相关文章

  • Python中使用PyMySQL库连接MySQL数据库
    一、什么是PyMysqlPyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,PyMySQL遵循Python数据库APIv2.0规范,并包含了pure-PythonMySQL客户端库。二、安装PyMysql可以使用命令pipinstallpymysql如果需要确定版本号,则可以使用以下命令pipinstall<包名......
  • kail 详细安装教程
    1.kail介绍KaliLinux是基于Debian的Linux发行版,设计用于数字取证操作系统。每一季度更新一次。由OffensiveSecurityLtd维护和资助。最先由OffensiveSecurity的MatiAharoni和DevonKearns通过重写BackTrack来完成,BackTrack是他们之前写的用于取证的Linux发行版。Kal......
  • Python中的装饰器
    一、装饰器的作用装饰器是Python中一种强大的编程工具,它允许我们在不修改原始函数代码的情况下,动态地增加功能或修改函数行为。装饰器提供了一种简洁而优雅的方式来修改、扩展或包装函数,使代码更具可读性和可维护性。装饰器的主要作用包括:添加额外的功能或逻辑,如日志记录、性......
  • python装饰器
    装饰器本质上是一个Python函数或类,它可以让其他函数或类在不需要做任何代码修改的前提下增加额外功能,装饰器的返回值也是一个函数/类对象Python中的函数可以像普通变量一样当做参数传递给另外一个函数,也可以把一个函数作为返回值,这类函数被称为高阶(Higher-order)函数它经常......
  • python HTTP Server 文件上传与下载
    pythonHTTPServer文件上传与下载实现在局域网(同一WIFI下)文件上传与下载该模块通过实现标准GET在BaseHTTPServer上构建和HEAD请求。(将所有代码粘贴到同一个py文件中,即可使用)所需包基于python3版本实现,python2版本无涉猎importosimportsysimportargparseimport......
  • postgresql从入门到精通教程 - 第36讲:postgresql逻辑备份
       PostgreSQL从小白到专家,是从入门逐渐能力提升的一个系列教程,内容包括对PG基础的认知、包括安装使用、包括角色权限、包括维护管理、、等内容,希望对热爱PG、学习PG的同学们有帮助,欢迎持续关注CUUGPG技术大讲堂。 第36讲:PostgreSQL逻辑备份 第36讲:12月02日(周六)19......
  • python装饰器
    importtimedefwrapper(type):print('start------',type)defoutter(fun):definner(*args,**kwargs):start_time=time.time()fun(*args,**kwargs)end_time=time.time()print('ru......
  • python助手
    python助手AssumetheroleofPyAssist,ahighlyskilledAIassistantspecializinginPythonprogramming.AsanexpertinthePythonlanguageanditsecosystem,yourmissionistoprovideguidance,support,andvaluableinsightstousersseekinghelpwithP......
  • 【Python】十六进制、八进制、二进制的写法
    1、十六进制语法:0x开头a=0x1122、八进制语法:0o开头a=0o1123、二进制语法:0b开头a=0b112......
  • 从零开始的 dbt 入门教程 (dbt-core 基础篇)
    最近一直在处理数据分析和数据建模的事情,所以接触了dbt等数据分析的工具,国内目前对于dbt比较详细的资料不多,所以打算写四道五篇dbt相关的文章,本文属于dbt系列的第一篇,本篇主要阐述dbt一些基本概念,教会你如何配置dbt连接远端数据库,并运行你的第一个数据模型,那么本文开......