【问题标题】:Python Split removing delimiters?Python拆分删除分隔符?
【发布时间】:2014-07-24 16:21:57
【问题描述】:

所以我有以下代码,在每个分号或 500 个字符之后放置一个 ~||~ 分隔符。这是有效的,但在找到分号时会删除它们。我看过这里,找到了答案,但我无法在我的代码中使用它。

chunk_len = 100
split_char = ';'
delim = ("~||~")
d = ";"
f = open(filename, "r")
text = f.read()
f.close()
lines = text.split(';')
for lines_idx, line in enumerate(lines):
    length = len(line)
    if length > chunk_len:
        chunks = [line[idx:idx+chunk_len]for idx in range(0,length,chunk_len)]
        lines[lines_idx] = delim.join(chunks)
new_text = delim.join(lines)
f = open(outputfile, 'w')
f.write(new_text)
f.close()

我在这里找到了这个解决方案,但我找不到将它合并到我的代码中的方法。对不起,重复的问题。

d = ">"
for line in all_lines:
    s =  [e+d for e in line.split(d) if e != ""]

【问题讨论】:

  • "doesnt work" 对我们来说毫无意义......你说的不能让它工作是什么意思?
  • 对不起。我尝试了两种不同的方法。有一次它保留了分号,但不是每 100 个字符的块。另一次它仍然删除了分号。抱歉不清楚。

标签: python split


【解决方案1】:

如果我正确理解了您的问题,那么您真正想要做的是在每个分号和每 500 个字符后插入您自己的分隔符。尝试分两步进行:

with open(filename, "r") as fi: # read in file using "with" statement
    text = fi.read()

block_size = 500            # sets how many characters separate new_delim
old_delim = ";"             # character we are adding the new delimiter to
new_delim = "~||~"          # this will be inserted every block_size characters
del_length = len(new_delim) # store length to prevent repeated calculations

for i in xrange(len(line)/block_size): 
    # calculate next index where the new delimiter should be inserted
    index = i*block_size + i*del_length + block_size

    # construct new string with new delimiter at the given index        
    text = "{0}{0}{1}".format(text[:index], new_delim, text[index:]) 

replacement_delim = old_delim + new_delim # old_delim will be replaced with this

with open(outputfile, 'w') as fo:
    # write out new string with new delimiter appended to each semicolon
    fo.write(text.replace(old_delim, replacement_delim))

如果分号恰好出现在 500 个字符的倍数处,您的两个特殊分隔符可能会彼此相邻。此外,如果您的字符串中正好有多个 block_size 字符,那么您将在字符串的末尾有分隔符。

此外,如果您正在读取很长的文件,这可能不是最好的方法。For 循环会在每次插入分隔符时创建一个全新的字符串

这种方法使 split 方法对分隔符的处理成为一个空点。

【讨论】:

  • 好的。我会试试这个方法。我必须稍微改变它,因为我正在运行 2.4.2,并且没有“with open”方法,但奇怪的是我知道如何改变它。谢谢。
【解决方案2】:

改变

lines = text.split(';')

lines = filter(None,re.split('([^;]+;)',text))

这应该保留分号......或者像其他答案一样稍后添加它

【讨论】:

  • 另一个答案有效,但我也会尝试这个。谢谢!
【解决方案3】:

split() 拆分字符串并删除分隔符,您只需将其重新添加即可。我在您的循环中执行以下操作: line = line + d

chunk_len = 100
split_char = ';'
delim = ("~||~")
d = ";"
f = open(filename, "r")
text = f.read()
f.close()
lines = text.split(';')
for lines_idx, line in enumerate(lines):
    line = line + d  #NEW LINE ADDED HERE
    length = len(line)
    if length > chunk_len:
        chunks = [line[idx:idx+chunk_len]for idx in range(0,length,chunk_len)]
        lines[lines_idx] = delim.join(chunks)
new_text = delim.join(lines)
f = open(outputfile, 'w')
f.write(new_text)
f.close()

【讨论】:

  • 这成功了!我知道让我看起来很愚蠢很简单。谢谢!我会投票,但我太菜鸟了。 :(
  • @user3754031,既然这是您的问题,您应该能够接受答案。您点击投票下方的复选框。
猜你喜欢
  • 2023-04-08
  • 2016-11-14
  • 2013-05-10
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 2021-12-07
  • 2019-02-01
相关资源
最近更新 更多