如何从文本文件中删除 \r 之间的文本,包括 \r?如果任何文本在 ' ' 之间包含 \r 也应该被删除
How to remove text in between \r's including \r from a text file?? And if any text contains \r in between ' ' that also should be removed
'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 '
您要做的是删除 \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
如何使用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)
'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 '
您要做的是删除 \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
如何使用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)