简体   繁体   English

如何用“||”替换第一个和最后一个“,”分隔符来自 csv 而其他人保持原样?

[英]How to replace the first and the last ',' delimiter with '||' from a csv while leaving the others as it is?

I have a CSV file:我有一个 CSV 文件:

101, "Name1", "Designation1", "blah1", "Blah1", 20200914001
102, "Name2", "Designation2", "blah2", "Blah2", 20200914002
103, "Name3", "Designation3", "blah3", "Blah3", 20200914003
104, "Name4", "Designation4", "blah4", "Blah4", 20200914004
105, "Name5", "Designation5", "blah5", "Blah5", 20200914005

replace each line like below:替换每一行,如下所示:

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

similar structure goes to the rest of the lines/records as well.类似的结构也适用于其余的行/记录。

My code replaces all of the delimiters.我的代码替换了所有分隔符。

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)

Thanks in advance.提前致谢。

Use

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

Replace with \\1||替换为\\1|| . . See proof .证明

Explanation解释

--------------------------------------------------------------------------------
  ^                        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 code : 蟒蛇代码

import re

regex = r'^([^,]*),|,(?=[^,]*$)'
test_str = r'101, "Name1", "Designation1", "blah1", "Blah1", 20200914001'
subst = r'\1||'
print(re.sub(regex, subst, test_str))

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

You could split on the first ( maxsplit=1 from the left) and last ( maxsplit=1 from the right) commas and join the results, eg:您可以拆分第一个(从左边开始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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将第一行的定界符从“,”替换为“;” 在Python的csv文件中 - Replace the delimiter of first line from ',' to ';' in csv file by Python 如何删除中间行离开第一个和最后一个 - How to drop intermediate lines leaving first and last 如何读取缺少列的 csv 文件并删除每行的第一个和最后一个分隔符? - how to read csv file with missing columns and remove first and last delimiter of each row? 如何使用熊猫在最后一个字段中存在分隔符的情况下读取CSV文件? - How to use Pandas to read CSV file with delimiter existing in the last field? 如何从 html 的 2 个标签中提取文本或替换第一个和最后一个标签 - How to extract text from 2 tags of html or replace first and last tag 在熊猫中使用to_csv函数时如何解决定界符错误 - How to solve the delimiter error while using to_csv function in pandas 无法用相应列中最后三行的平均值替换数据帧最后一行中的零,同时保留非零值 - unable to replace zeros in the last row of dataframe with mean of last three rows in respective columns while leaving non zero values as it is 如何只将拉丁字符大写,而让其他字符保持原样? - How to uppercase only Latin characters, leaving others as it is? 如何使用python更改csv文件的定界符,同时还剥离新定界符的字段? - How can I use python to change the delimiter of a csv file while also stripping the fields of the new delimiter? 在python中保留第一行和最后一行元素 - Leaving first row and last row element in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM