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)