2016-08-18 54 views
1

我有这样的格式的文本文件:如何解析某些文本数据?

B2100 Door Driver Key Cylinder Switch Failure B2101 Head Rest Switch Circuit Failure B2102 Antenna Circuit Short to Ground`, plus 1000 lines more.

这是我希望它是:

B2100*Door Driver Key Cylinder Switch Failure B2101*Head Rest Switch Circuit Failure B2102*Antenna Circuit Short to Ground B2103*Antenna Not Connected B2104*Door Passenger Key Cylinder Switch Failure

,这样我可以在LibreOffice的Calc中复制此数据和它会将其格式化为两列代码并分别表示它们的含义。

我的思维过程:
套用正规快件超过Bxxxx,把一个星号在它的前面(它作为一个分隔符)和\n之前的含义(?我不知道这是否会工作),并删除空白,直到遇到下一个字符。

我正在尝试隔离B2100,直到现在都失败了。我天真的尝试:

import re 

text = """B2100 Door Driver Key Cylinder Switch Failure B2101 Head Rest Switch Circuit Failure B2102 Antenna Circuit Short to Ground B2103 Antenna Not Connected B2104 Door Passenger Key Cylinder Switch Failure B2105 Throttle Position Input Out of Range Low B2106 Throttle Position Input Out of Range High B2107 Front Wiper Motor Relay Circuit Short to Vbatt B2108 Trunk Key Cylinder Switch Failure""" 
# text_arr = text.split("\^B[0-9][0-9][0-9][0-9]$\gi"); 
l = re.compile('\^B[0-9][0-9][0-9][0-9]$\gi').split(text) 
print(l) 

此输出:

['B2100\tDoor Driver Key Cylinder Switch Failure B2101\tHead Rest Switch Circuit Failure B2102\tAntenna Circuit Short to Ground B2103\tAntenna Not Connected B2104\tDoor Passenger Key Cylinder Switch Failure B2105\tThrottle Position Input Out of Range Low B2106\tThrottle Position Input Out of Range High B2107\tFront Wiper Motor Relay Circuit Short to Vbatt B2108\tTrunk Key Cylinder Switch Failure'] 

如何达到预期的效果?

为了进一步打破它,我想要做的是这样的:
打破一切都变成代码(B1001)和含义(后文)阵列,然后将其应用于每个操作(\n的东西)个别。如果你对如何做整件事有更好的想法,那就更好。我很想听到它。

+0

是有...但它似乎是随机的。 –

+0

'replace('B21','\ nB21')'? –

回答

5

基本上,你想:

  • 查找输入任何Bxxxx字符串。
  • 用换行符替换它们之前的任何空格。
  • *替换后面的空格。

这些都可以用一个单一的re.sub()完成:

re.sub(r'\s*(B\d{4})\s*', r'\n\1*', text).strip() 

匹配模式:

\s*    # Any amount of whitespace 
    (B\d{4})  # "B" followed by exactly 4 digits 
      \s* # Any amount of whitespace 

替换模式:

\n    # Newline 
    \1    # The first parenthesized sequence from the matching pattern (B####) 
    *   # Literal "*" 

strip()的目的是修剪任何领先或tr生病的空白,包括将从第一个B ####序列的子部分产生的换行符。

+0

你有一些正则表达式忍者?该死的工作......谢谢一吨 –

0

首先,你的正则表达式是错误的 “\^B [0-9] [0-9] [0-9] [0-9] $ \ GI

  1. 修饰符不工作这种方式在Python上
  2. ^和$表示行的开始和结束,它不会匹配文本上的任何内容
  3. 可将多个[0-9]替换为[[0-9] { 4}'
  4. 如果你想忽略大小写使用Python上的记者事物regex

考虑到这一点简单的代码来实现你想要的是这样的:

l = [x.strip() for x in re.compile('\s*(B\d{4})\s*', re.IGNORECASE).split(text)] 
lines = ['*'.join(l[i:i+2]) for i in range(0,len(l),2)] 
+0

不是一个真正的答案 – Frodon

0
import re 
text = """B2100 Door Driver Key Cylinder Switch Failure B2101 Head Rest Switch Circuit Failure B2102 Antenna Circuit Short to Ground B2103 Antenna Not Connected B2104 Door Passenger Key Cylinder Switch Failure B2105 Throttle Position Input Out of Range Low B2106 Throttle Position Input Out of Range High B2107 Front Wiper Motor Relay Circuit Short to Vbatt B2108 Trunk Key Cylinder Switch Failure""" 

l = [i for i in re.split('(B[0-9]{4}\s+)', text) if i] 
print '\n'.join(['{}*{}'.format(id_.strip(), label.strip()) for id_,label in zip(l[0::2], l[1::2])]) 

.split可以分割后保留的分隔符,如果你有()在你的正则表达式。上述方法产生的输出:

B2100*Door Driver Key Cylinder Switch Failure 
B2101*Head Rest Switch Circuit Failure 
B2102*Antenna Circuit Short to Ground 
B2103*Antenna Not Connected 
B2104*Door Passenger Key Cylinder Switch Failure 
B2105*Throttle Position Input Out of Range Low 
B2106*Throttle Position Input Out of Range High 
B2107*Front Wiper Motor Relay Circuit Short to Vbatt 
B2108*Trunk Key Cylinder Switch Failure 
0

进口重新

进口熊猫作为PD

轻拍= R “(B \ d +)”

ZZZ = [I对于i在re.split(PAT,KKK)如果我! ='']

pd.DataFrame({'Col1':zzz [:: 2],'Col2':[i.strip()for i in zzz if re.match(pat,i)is None] })

Col1 Col2

0 B2100车门驱动锁芯开关故障

1 B2101头枕开关电路故障

2 B2102天线电路接地短路 3 B2100车门驱动锁芯开关故障