亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用“||”替換第一個和最后一個“,”分隔符 來自 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 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號