数据:

对txt文件进行数据处理:

txt_file_path = "basic_info.txt"
write_txt_file_path = "basic_info1.txt"
def write_txt_file():
if os.path.exists(txt_file_path) is False:
return
with open(txt_file_path,'r') as r_file:
for row in r_file:
list = row.split("\t")
# print("用\\t分割行的结果:{}".format(list))
# print("待分割的列:{}".format(list[2]))
col = list[2]
res = col[2:-2].split('|')
# print("对此列进行分割的结果:{}".format(res))
#将分割结果写入新的文件中
with open(write_txt_file_path,'a') as w_file:
w_file.write(' '.join(res))
w_file.write('\n')
#break if __name__ == "__main__":
write_txt_file()

  

python 数据处理 对txt文件进行数据处理的更多相关文章

  1. python 简单的txt文件读写

    1 读取txt文件.跟c相比,python的文件读写简直是方便的可怕 首先是读取文件 首先获得文件名称,然后通过 open函数打开文件,通过for循环逐行读出文件内容 #!python file by ...

  2. python导入csv/txt文件

    1. 导入csv文件 ### python导入csv文件的三种方法 ```python #原始的方式 lines = [line.split(',') for line in open('iris.c ...

  3. python应用:TXT文件的读写

    python读写TXT文件不需要导入包 python中常用的读写方式: 文件打开模式 描述 r 以只读模式打开文件,并将文件指针指向文件头:如果文件不存在会报错 w 以只写模式打开文件,并将文件指针指 ...

  4. python批量读取txt文件为DataFrame

    我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...

  5. python中写入txt文件需要换行,以及\r 和\n

    在Python中,用open()函数打开一个txt文件,写入一行数据之后需要一个换行 如果直接用 f.write(’\n’)只会在后面打印一个字符串’\n’,而不是换行’需要用 f.write(’\r ...

  6. Python - 生成 requirement.txt 文件

    前言 Python项目中,一般都会有一个 requirements.txt 文件 这个文件主要是用于记录当前项目下的所有依赖包及其精确的版本号,以方便在一个新环境下更快的进行部署 如何生成 requi ...

  7. Python读取中文txt文件错误:UnicodeEncodeError: 'gbk' codec can't encode character

    with open(file,'r') as f: line=f.readline() i=1 while line: line=line.decode('utf-8') line=f.readlin ...

  8. python基础===取txt文件的若干行到另一个文件

    #取txt文件 的若干行到另一个txt f1 = open(r'F:\movie.txt','rb') f2= open(r'F:\movie2.txt','ab') i=0 while True: ...

  9. python 逐行读取txt文件

    逐行读取txt文件 path = r'D:\123456\1.txt'with open(path, 'r', encoding='utf-8') as f:    for line in f:   ...

随机推荐

  1. Django 学习之Rest Framework 视图集与Routers与扩展功能

    一.视图集使用 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中: list() 提供一组数据 retrieve() 提供单个数据 create() 创建数据 update() 保存数 ...

  2. MySQL之innodb和myisam的区别

    innodb和myisam的区别: MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型, .frm文件存储表定义, 数据文件的扩展名为.MYD, 索引文件的扩展名是 ...

  3. centos将uwsgi添加为系统服务

    如果退出ssh 链接, 都会导致uwsgi进程关闭 这时, 我们需要进行管理软件管理uwsgi进行的运行, centos系统中我们采用 systemd, 让我们的项目变为系统服务 第一步: 首先 vi ...

  4. loadrunner 接口测试实战

    直接上代码: web_reg_save_param("Name",   //这个函数是为了获取服务器返回的值.我这个接口的返回值是这样子的 //将服务器返回的值放在Name里,Na ...

  5. IELTS Writing Task 1: two-chart answer

    Thursday, January 09, 2020 The chart below shows the value of one country's exports in various categ ...

  6. SpringBoot之日志记录-专题四

    SpringBoot之日志记录-专题四 六.日志管理 6.1使用log4j记录日志 6.1.2新建log4j配置文件 文件名称log4j.properties #log4j.rootLogger=CO ...

  7. WebMagic基础与Maven管理依赖

    2. 快速开始 WebMagic主要包含两个jar包:webmagic-core-{version}.jar和webmagic-extension-{version}.jar.在项目中添加这两个包的依 ...

  8. 3.Sprint 代理对象与原始对象的异常错误

    代码案例分析 Service层添加了注解@Transactional @Service @Transactional public class CustomerService extends Base ...

  9. No module named 'widgets'

    https://blog.csdn.net/heatdeath/article/details/70313645 适配python3的. https://github.com/twz915/Djang ...

  10. Python 类型转换指南

    一.int型 支持转换为 int 类型的,仅有 float.str.bytes,其他类型均不支持. 1.float -> int会去掉小数点及后面的数值,仅保留整数部分. 2.str -> ...