Python 提取/剔除任意列
18 Jun 2019 最近遇到了需要批量剔除某列字段的需求,虽然很简单,但想着今后可能还会复用,遂在此记录一下。
原始数据下载地址:test_dir.zip
Python实现代码如下:
# ==== 单个文件提取任意字段 ====
import os
ftxt = "D:\\test_dir\\test01.txt"
contents = []
with open(ftxt, "r", encoding="utf-8") as f:
mylist = list(f) # 将字符串转化为数组
for i in mylist:
content = i.replace("\n", "").split("\t") # 将数据划分为不同的列
s = "".join(content[2]+"\t"+content[3]) # 提取/剔除任意列
contents.append(s)
with open(ftxt, "w", encoding="utf-8") as f: # 将结果写入文件
if isinstance(contents, list):
for content in contents:
f.write("{0}{1}".format(content, os.linesep))
else:
f.write(contents)
# ==== 批量提取任意字段 ====
import os
ftxts = "D:\\test_dir\\"
for root, dirs, files in os.walk(ftxts):
for n in files:
ftxt = root + n
contents = [] # 注意contents位置
with open(ftxt, "r", encoding="utf-8") as f:
mylist = list(f) # 将字符串转化为数组
for i in mylist:
content = i.replace("\n", "").split("\t") # 对数据进行划分
s = "".join(content[-2] + "\t" + content[-1]) # 提取任意字段
contents.append(s)
with open(ftxt, "w", encoding="utf-8") as f:
if isinstance(contents, list):
for content in contents:
f.write("{0}{1}".format(content, os.linesep))
else:
f.write(contents)
分类: Python技术