为了账号安全,请及时绑定邮箱和手机立即绑定

如何用“||”替换第一个和最后一个“,”分隔符 来自 csv 而保留其他内容不变?

如何用“||”替换第一个和最后一个“,”分隔符 来自 csv 而保留其他内容不变?

MM们 2023-08-08 09:58:59
我有一个 CSV 文件:101, "Name1", "Designation1", "blah1", "Blah1", 20200914001102, "Name2", "Designation2", "blah2", "Blah2", 20200914002103, "Name3", "Designation3", "blah3", "Blah3", 20200914003104, "Name4", "Designation4", "blah4", "Blah4", 20200914004105, "Name5", "Designation5", "blah5", "Blah5", 20200914005将每一行替换如下:101|| "Name1", "Designation1", "blah1", "Blah1"|| 20200914001类似的结构也适用于其余的行/记录。我的代码替换了所有分隔符。data = ""with open('firstCSV.csv', 'r') as file:    data = file.read().replace(',', '||').replace(' ', '')with open("first_Out.csv", "w") as out_file:    out_file.write(data)提前致谢。
查看完整描述

2 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

使用

^([^,]*),|,(?=[^,]*$)

替换为\1||. 见证明。

解释

--------------------------------------------------------------------------------

  ^                        the beginning of the string

--------------------------------------------------------------------------------

  (                        group and capture to \1:

--------------------------------------------------------------------------------

    [^,]*                    any character except: ',' (0 or more

                             times (matching the most amount

                             possible))

--------------------------------------------------------------------------------

  )                        end of \1

--------------------------------------------------------------------------------

  ,                        ','

--------------------------------------------------------------------------------

 |                        OR

--------------------------------------------------------------------------------

  ,                        ','

--------------------------------------------------------------------------------

  (?=                      look ahead to see if there is:

--------------------------------------------------------------------------------

    [^,]*                    any character except: ',' (0 or more

                             times (matching the most amount

                             possible))

--------------------------------------------------------------------------------

    $                        before an optional \n, and the end of

                             the string

--------------------------------------------------------------------------------

  )                        end of look-ahead

Python代码

import re


regex = r'^([^,]*),|,(?=[^,]*$)'

test_str = r'101, "Name1", "Designation1", "blah1", "Blah1", 20200914001'

subst = r'\1||'

print(re.sub(regex, subst, test_str))

结果:101|| "Name1", "Designation1", "blah1", "Blah1"|| 20200914001.


查看完整回答
反对 回复 2023-08-08
?
隔江千里

TA贡献1906条经验 获得超10个赞

您可以拆分第一个(maxsplit=1从左起)和最后一个(maxsplit=1从右起)逗号并连接结果,例如:


>>> line = '101, "Name1", "Designation1", "blah1", "Blah1", 20200914001'


>>> first, rest = line.split(',', maxsplit=1)

>>> rest, last = rest.rsplit(',', maxsplit=1)

>>> '||'.join((first, rest, last))

'101|| "Name1", "Designation1", "blah1", "Blah1"|| 20200914001'


查看完整回答
反对 回复 2023-08-08
  • 2 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信