简体   繁体   English

Python 用逗号将一个项目拆分(?)为 csv

[英]Python split(?) an item into multiple with comma for csv

I have the following output我有以下 output

123399383 (blahthing1)(blahthing2)(blahthing3)(blahthing4) 123399383 (blahthing1)(blahthing2)(blahthing3)(blahthing4)

I tried using replace to replace the () with a comma which worked but its still a single item and the entire line shows up in a single cell of my csv.我尝试使用替换用逗号替换(),但它仍然是一个项目,整行显示在我的 csv 的单个单元格中。 What I'd like is我想要的是

123399383,blahthing1,blahthing2,blahthing3,blahthing4 123399383,blahthing1,blahthing2,blahthing3,blahthing4

So each is a separate cell in my csv.所以每个都是我的 csv 中的一个单独的单元格。 Example is one of hundreds of lines I'm going through.示例是我正在经历的数百行之一。 Thanks for the time and any help you can throw me.谢谢你的时间和任何帮助你可以扔给我。

For your exact type of string, we can use re.findall here for a regex based approach:对于您确切的字符串类型,我们可以在此处使用re.findall来实现基于正则表达式的方法:

inp = "123399383 (blahthing1)(blahthing2)(blahthing3)(blahthing4)"
output = ','.join(re.findall(r'\w+', inp))
print(output)  # 123399383,blahthing1,blahthing2,blahthing3,blahthing4

re.split() will let you split on the specific characters you have. re.split()将让您拆分您拥有的特定字符。 This will allow non-word characters to exist in the strings:这将允许字符串中存在非单词字符:

import re

s = '123399383 (blah++thing1)(blaht-&^hing2)(blah  thing3)(blahthing4)'

# split on space or closing parenthesis      
# and opening parentheses   
re.split(r'[\s\)]\(', s)

# ['123399383', 'blah++thing1', 'blaht-&^hing2', 'blah  thing3', 'blahthing4)']

An alternative way to solve it using regex is;使用正则表达式解决它的另一种方法是;

original = "123399383(blahthing1)(blahthing2)(blahthing3)(blahthing4)"
new = re.sub("\W+", ",", s)[:-1]
print(new)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM