如何使用Python在搜索的单词或字符上分割线条并保留字符

时间:2017-01-27 07:23:51

标签: python python-2.7

当我搜索字符串或字符时,我试图将剩余的单词追加或放入下一行。 例如,我有一个文件,其中包含如下所示的段落:

/This is my first time doing this\ /I am trying to append this line\
/I can't seem to find the solution\ /Trying so hard\ /Testing so hard\

我想搜索/,结果如下:

/This is my first time doing this\
/I am trying to append this line\
/I can't seem to find the solution\
/Trying so hard\
/Testing so hard\

因此,只要它在行中看到前向闪烁/,它就会将该行中剩余的字符串或文本移动到下一行。我想将它们收集到格式列表中,以便我可以进行进一步处理。

3 个答案:

答案 0 :(得分:1)

以下是将文字拆分为行列表的一种方法。

data = r'''/This is my first time doing this\ /I am trying to append this line\
/I can't seem to find the solution\ /Trying so hard\ /Testing so hard\
'''

print(data)
print('- ' * 20)

# Put text after `/` onto a new line
data = data.replace('/', '\n/')

# Split text up into a list of lines
lines = data.splitlines()

# Remove leading and trailing whitespace from each line
lines = [s.strip() for s in lines]

# Get rid of empty lines
lines = [s for s in lines if s]

for line in lines:
    print(line)

<强>输出

/This is my first time doing this\ /I am trying to append this line\
/I can't seem to find the solution\ /Trying so hard\ /Testing so hard\

- - - - - - - - - - - - - - - - - - - - 
/This is my first time doing this\
/I am trying to append this line\
/I can't seem to find the solution\
/Trying so hard\
/Testing so hard\

为了确保每一行完全我们想要的内容,我们可以打印它的表示:

# show the representation of each line
for line in lines:
    print(repr(line))

<强>输出

'/This is my first time doing this\\'
'/I am trying to append this line\\'
"/I can't seem to find the solution\\"
'/Trying so hard\\'
'/Testing so hard\\'

请注意,单个反斜杠在表示中显示为一对反斜杠。同样,要将反斜杠放入普通的Python字符串文字中,您需要使用两个反斜杠,例如

s = 'One backslash \\ here'

我不需要在data字符串文字中执行此操作,因为我使用了raw string

要从文本文件中读取data而不是使用字符串文字,您只需使用文件.read方法,如下所示:

with open('test.txt') as f:
    data = f.read()
print(data)

<强>输出

/This is my first time doing this\ /I am trying to append this line\
/I can't seem to find the solution\ /Trying so hard\ /Testing so hard\

当您完成处理lines列表并希望将其保存到新文件后,最简单的方法是将文件参数传递给print。例如,

with open('sample3.txt', 'w') as outfile:
    for line in lines: 
        print(line, file=outfile)

我还应该提一下,要在Python 2中使用print 函数而不是print 语句,那么您需要导入它。为此,请在其他导入之前将此行放在脚本的顶部:

from __future__ import print_function

实际上,告诉翻译使用Python 3风格的分区是个好主意:

from __future__ import print_function, division

答案 1 :(得分:-1)

您是否考虑过这样做,例如您有一个段落,第二句话的字符为&#39; /&#39;。

def sampleFunction():

    text = "/"
    newText = "\n"

    x = fileinput.input(files ="log.txt", inplace=1)
    for line in x:
        if text in line:
            line = newText
        sys.stdout.write(line)

这样以&#39; /&#39;开头的行将在下一行。

答案 2 :(得分:-1)

你可以再试一次。将字符替换为下一行

  ID  Zeit600  Zeit650  Zeit700  Zeit750  Zeit800 T800_T600 T800_T650 T800_T700 T800_T750 Abkuehlrate_T800_600 Abkuehlrate_T800_650
1  1 601.7826 504.7059 321.2658 264.3038 207.3418  394.4408  297.3641  113.9241  56.96203            0.5070469            0.5044321
2  2 602.6250 546.6667 316.6667 261.1111 205.5556  397.0694  341.1111  111.1111  55.55556            0.5036902            0.4397394
  Abkuehlrate_T800_700 Abkuehlrate_T800_750
1            0.8777778            0.8777778
2            0.9000000            0.9000000