替换引号中的逗号以外的逗号 [英] Replace commas except those in quotation marks

查看:83
本文介绍了替换引号中的逗号以外的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Date,Time,Ref,Sen,ATN,Flow,PCB temp,Status,Battery,BC
2015/04/23,12:30:00,779581,908043,"-15,254",49,31,0,100,
2015/04/23,12:35:00,778715,907084,"-15,259",49,31,0,100,-127
2015/04/23,12:40:00,778299,906419,"-15,239",49,32,0,100,461
(...)

我有一个像上面一样的 ascii 文件,我试图用逗号替换分号.这是我正在使用的代码:

Hi, I have an ascii file like the one above where I am trying to replace the commas for semicolon. This is the code I am using:

filein = open('Prueba1.txt')
fileout = open('Fin.txt', 'wt')
for line in filein:
    if line.startswith('20'):
        fileout.write( line.replace(',', ';') )
filein.close()
fileout.close()

问题是现在我想保留第 5 列的逗号并保留引号.有什么想法吗?

The problem is that now I want to maintain the commas for the 5th column and also take of the quotation marks. Any ideas?

推荐答案

您是否考虑过将其保存为 csv 文件.

Have you considered saving this as a csv file.

保存为 csv 文件后:

after saving it as csv file:

import pandas as pd
v = pd.read_csv("check_delete.csv")

输出:

      Date            Time  Ref     Sen      ATN      Flow  PCB temp    Status  Battery BC
0   2015/04/23  12:30:00    779581  908043  -15,254 49  31  0   100   NaN
1   2015/04/23  12:35:00    778715  907084  -15,259 49  31  0   100   -127.0
2   2015/04/23  12:40:00    778299  906419  -15,239 49  32  0   100    461.0

您可以将其作为数据帧读取.

You can read this as a dataframe.

这篇关于替换引号中的逗号以外的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆