python处理txt文件_python 数据处理 对txt文件进行数据处理

本文介绍了如何使用Python读取txt文件,通过' '分隔行,并针对特定列中的'|'符号进行进一步分割,最终将处理后的结果写入新文件的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据:

20191019180458016131.png

对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()

原文:https://www.cnblogs.com/jiaxinwei/p/11704561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值