I am writing a python script to extract data from series of email from newsgroups files in jupyter notebook.
I got the following response
[“/bin/bash: -c: line 0: syntax error near unexpected token |'", '/bin/bash: -c: line 0:
cat <_io.TextIOWrapper name=’/home/opc/sci.med/59383′ mode=’a’ encoding=’UTF-8′> | awk “/Newsgroups/ {print $0}””]
when I open the file using the with open() syntax like
with open('/home/opc/sci.med/59383', 'r') as f:
Newsgroups = ! cat {f} | awk "/Newsgroups/ {{print $$0}}"
print (Newsgroup)
I got the following response
[“/bin/bash: -c: line 0: syntax error near unexpected token |'", '/bin/bash: -c: line 0:
cat <_io.TextIOWrapper name=’/home/opc/sci.med/59383′ mode=’r’ encoding=’UTF-8′> | awk “/Newsgroups/ {print $0}””]
Please is there a way I can get this to work?
I tried to open the files without the with syntax though and it worked fine.
With the format below:
files = '/home/opc/sci.med/59383'
Newsgroups1 = ! cat {files} | awk '/Newsgroups/ {{ print $$0}}'
Subject = ! cat {files} | awk '/Subject/ {{ print $$0 }}'
Message = ! cat {files} | awk '/Lines/,EOF {{ print $$0 }}'
print(Newsgroups1)
print(Subject)
print(Message)
I got the expected response, able to extract the string in each text field
[‘Newsgroups: sci.med’]
[‘Subject: Re: HELP for Kidney Stones …………..’]
Lines: 28 In article <etxmow.735561695@garboc29 [email protected] (Mats Winberg) writes: Isn’t there a relatively new treatment for kidney stones involving a non-invasive use of ultra-sound where the patient is lowered into some sort of liquid when…
I like to understand why this happens and is there a way to get the with wrapper to work so I don’t have to manage the resource usage manually
E. Akinpelu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.