Python处理表格型txt文件


从数据库中导出的txt文件,数据排列类似于表格形式,中间有制表符\t分隔开(tab键)
col1 col2 col3
1 2 3
1 2 3
1 2 3

1.pandas读txt

import pandas as pd
data = pd.read_table(txt_file_path, sep='\t',engine='python')

加载file,指定它的分隔符是 \t 指定engine=python,处理中文路径引起的错误
除了read_table可以读取txt,read_csv也可以读取

data = pd.read_csv(txt_file_path, sep='\t',engine='python')
data = pd.read_csv(csv_file_path, sep='\t',engine='python')

同时read_table可以读取csv文件,当相同的文件内容以txt和csv两种格式保存时,可以用相同的代码同时处理两种格式的文件

import pandas as pd
data = pd.read_table(csv_file_path, sep='\t',engine='python')

https://www.jb51.net/article/182027.htm read_table
https://blog.csdn.net/weixin_39687783/article/details/80792883 read_csv
https://blog.csdn.net/qq_35318838/article/details/80564938 参数中有中文出错,添加参数engine=‘python’

2.获取某列所有数据

with open('dest_file.txt', 'w', encoding='utf-8')as f:
    for one in data["col"]:
        content = one + '\n'
        f.write(content)

3.指定两列分别做键值-字典,判断想要的查询数据-键对应的值-value

以某列为键,就已经对该列做了去重

dictionary = data.set_index("col1").to_dict()["col2"]

https://blog.csdn.net/weixin_42831564/article/details/105406396

with open('待查询keys.txt', 'r')as f, open('./result.txt', 'w', encoding='utf-8')as f_w:
    for line in f:
        line = line.strip()
        if line in dictionary.keys():
        	value = dicionary[line]
            content = line + str(value) + '\n'
            f_w.write(content)

https://www.cnblogs.com/xmnote/p/9334880.html 判断字典是否存在某个key
https://blog.csdn.net/maomaona/article/details/85119447 pandas数据类型转化成想要的–str(value)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值