2015-11-26 57 views
-1
'Aadhirai' 'A special star' '6' 'Boy' '' "\rgoogletag.cmd.push(function() { googletag.display('div-gpt-ad-1445572280350-0'); });\r" 'Aadhiren' 'Dark' '6' 'Boy' '' 'Aadhish' 'King Commanded Counselled' '5' 'Boy' '' 'Aadhyatm' 'Dhyan' '1' 'Boy' '' 'Aadi' 'First Most important Beginning Ornament Adornment' '6' 'Boy' '' 'Aadia' 'Being a gift' '7' 'Boy' '' 'Aadidev' 'The first God' '1' 'Boy' '' 'Aadijay' 'The first victory' '6' 'Boy' '' 'Aadim' 'Entire universe' '1' 'Boy' '' 'Aadinath' 'The first Lord Lord Vishnu' '4' 'Boy' '' 'Aadipta' 'Bright' '7' 'Boy' '' 'Aadish' 'Full of wisdom Intelligent' '6' 'Boy' '' 'Aadishankar' 'Sri shankaracharya Founder of Adwaitha philosophy' '6' 'Boy' '' 'Aadit' 'Peak Lord of Sun' '8' 'Boy' '' 'Aaditey' 'Son of Aditi' '11' 'Boy' '' '\r  (adsbygoogle = window.adsbygoogle ||).push({});\r ' 
+1

1)請使用代碼格式化,和2),您可以張貼代碼你到目前爲止? –

+0

你應該顯示你的預期輸出和你到目前爲止做了什麼 – The6thSense

+0

'string.replace('\ r','')'? – SIslam

回答

1

你想要做的是刪除\r and another \r之間的數據。這裏使用的正確的東西將是正則表達式。

代碼:

import re 
check="""'Aadhirai' 'A special star' '6' 'Boy' '' "\rgoogletag.cmd.push(function() { googletag.display('div-gpt-ad-1445572280350-0'); });\r" 'Aadhiren' 'Dark' '6' 'Boy' '' 'Aadhish' 'King Commanded Counselled' '5' 'Boy' '' 'Aadhyatm' 'Dhyan' '1' 'Boy' '' 'Aadi' 'First Most important Beginning Ornament Adornment' '6' 'Boy' '' 'Aadia' 'Being a gift' '7' 'Boy' '' 'Aadidev' 'The first God' '1' 'Boy' '' 'Aadijay' 'The first victory' '6' 'Boy' '' 'Aadim' 'Entire universe' '1' 'Boy' '' 'Aadinath' 'The first Lord Lord Vishnu' '4' 'Boy' '' 'Aadipta' 'Bright' '7' 'Boy' '' 'Aadish' 'Full of wisdom Intelligent' '6' 'Boy' '' 'Aadishankar' 'Sri shankaracharya Founder of Adwaitha philosophy' '6' 'Boy' '' 'Aadit' 'Peak Lord of Sun' '8' 'Boy' '' 'Aaditey' 'Son of Aditi' '11' 'Boy' '' '\r  (adsbygoogle = window.adsbygoogle ||).push({});\r '""" 
print re.sub(r"\r.*?\r"," ",check) 

輸出:

'Aadhirai' 'A special star' '6' 'Boy' '' " " 'Aadhiren' 'Dark' '6' 'Boy' '' 'Aadhish' 'King Commanded Counselled' '5' 'Boy' '' 'Aadhyatm' 'Dhyan' '1' 'Boy' '' 'Aadi' 'First Most important Beginning Ornament Adornment' '6' 'Boy' '' 'Aadia' 'Being a gift' '7' 'Boy' '' 'Aadidev' 'The first God' '1' 'Boy' '' 'Aadijay' 'The first victory' '6' 'Boy' '' 'Aadim' 'Entire universe' '1' 'Boy' '' 'Aadinath' 'The first Lord Lord Vishnu' '4' 'Boy' '' 'Aadipta' 'Bright' '7' 'Boy' '' 'Aadish' 'Full of wisdom Intelligent' '6' 'Boy' '' 'Aadishankar' 'Sri shankaracharya Founder of Adwaitha philosophy' '6' 'Boy' '' 'Aadit' 'Peak Lord of Sun' '8' 'Boy' '' 'Aaditey' 'Son of Aditi' '11' 'Boy' '' '  ' 

注:

  • re模塊用於做regex匹配
  • \r.*?\r是我想匹配它start from \r match everything until next \r
+0

它的完美性很好..但是您能否幫助我刪除也存在\ r的引號..即完全刪除包含引號的字符串 –

+0

引用引號想刪除所有的引號都只是\ r引號 – The6thSense

0
file = open('/home/rohit/Desktop/output.txt', 'r') 
fileout1 = open('/home/rohit/Desktop/output1.txt','w') 
for char in file: 
    char = char.replace("\\n","").split(',') 
    for i in range(len(char)): 
      fileout1.write(char[i]) 

仍然得到不需要的文本,如「\ RCV vjkv denrtgfhhjg \ r」 ..想從output1.txt

刪除這些數據
+0

你可以添加這個在你的問題[編輯](http://stackoverflow.com/posts/33931873 /編輯)並且不填充答案部分:) – The6thSense

0

說如何使用filter正則表達式:

"define filtering function" 
good = lambda x : not(x.startswith("\r") and x.endswith()) 
"use with statement with open!" 
with open('/home/rohit/Desktop/output.txt', 'r') as filein: 
    with open('/home/rohit/Desktop/output1.txt','w') as fileout1: 
     for line in filein: 
      cols = line.rstrip("\n").split(',') 
      "remove unwanted columns" 
      cols = list(filter(good , cols)) 
      for c in cols: 
       fileout1.write(c) 
相關問題