I can't interpret the error message when trying to use PyPDF2 in WinPython/Spyder
Error message:
In [3]: runfile('C:/Users/User/Downloads/WPy64-3720/pdf2text.py', wdir='C:/Users/User/Downloads/WPy64-3720')
Code:
import PyPDF2
path="C:\\Users\User\Downloads\WPy64-3720\soln.pdf"
text="C:\\Users\User\Downloads\WPy64-3720\Soln.text"
pdf_file = open(path, 'rb')
text =""
read_pdf = PyPDF2.PdfFileReader(pdf_file)
c = read_pdf.numPages
for i in range(c):
page = read_pdf.getPage(i)
text+=(page.extractText())
(Spyder maintainer here) That's not an error message. runfile is the command used by Spyder to run your code in the IPython console.
If you're not seeing anything else is because you haven't printed anything in your code. For instance, adding this line to your code
print('Hello world!')
will show a Hello world printed below runfile.
Related
Trying to do a local blast using NCBIBLAST+, already downloaded in the mentioned path. I am using Win10 and Spyder.
from Bio.Blast.Applications import NcbiblastpCommandline
blastp=r"C:\NCBI\blast-BLAST_VERSION+\bin\blastp.exe"
blastp_cline= NcbiblastpCommandline(blastp,query="test.fasta",db='Bos_transcript_and_prot\\bos_protein.fasta',outfmt=5,evalue=0.001, out="test.xml")
blastp_cline
from Bio.Blast import NCBIXML
with open("test.xml") as result_handle:
E_VALUE_THRESH=0.01
blast_records = NCBIXML.parse(result_handle)
blast_record = NCBIXML.read(result_handle)
for alignment in blast_record.alignments:
for hsp in alignment.hsps:
if hsp.expect < E_VALUE_THRESH:
print("****Alignment****")
print("sequence:", alignment.title)
print("length:", alignment.length)
print("e value:", hsp.expect)
print(hsp.query[0:75] + "...")
print(hsp.match[0:75] + "...")
print(hsp.sbjct[0:75] + "...")
I have my database created as well as the fasta files contain only protein sequences. Still .XML file is not getting created and also the blastp_cline variable is not getting created.
Thanks in advance
For execution of the command use:
stdout, stderr = blastp_cline()
try:
change with open("test.xml") as result_handle:
with :
with open("test.xml", 'w') as result_handle:
and let me know
dont remember how 'w' differes from 'w+' , try to google it
I'm writing an AppleScript that I'm going to run in a macOS app. This script works fine in Script Editor but when I bring it into Xcode and try to run it, encoding a URL fails with an error about quotation marks.
I'd love any tips to get this to run successfully via Swift and Xcode rather than just Script Editor. Thanks!
My code:
var source = """
on encode(str)
do shell script "php -r 'echo urlencode(\"" & str & "\");'"
end encode
set f to encode("https://twitter.com")
f
"""
let script = NSAppleScript(source: source)!
var error: NSDictionary? = nil
let result = script.executeAndReturnError(&error)
print(result.stringValue)
print(error)
Error prints out:
Optional({
NSAppleScriptErrorBriefMessage = "Expected end of line but found \U201c\"\U201d.";
NSAppleScriptErrorMessage = "Expected end of line but found \U201c\"\U201d.";
NSAppleScriptErrorNumber = "-2741";
NSAppleScriptErrorRange = "NSRange: {60, 1}";
})
Quoting strings in shell scripts can be annoying.
Try this, quoted form of looks for the best combination
do shell script "php -r " & quoted form of ("echo urlencode(" & quote & str & quote & ");")
By the way NSRange: {60, 1} tells you where the error occurs (the 61st character in the string)
Using Python 3.5 I have created code to process a file and write output to another file. Following is relevant code;
with open('2016_01_22_Investor_Companies_stops.txt','r') as stops_Investor_Companies:
stops_Investor_Companies = stops_Investor_Companies.read()
stops_Investor_Companies = nltk.word_tokenize(stops_Investor_Companies)
stops_Investor_Companies= [w.lower() for w in stops_Investor_Companies]
stops_Investor_Companies = str(stops_Investor_Companies)
outfile = open ('stops_Investor_Companies_cln.txt', 'w')
outfile.write(stops_Investor_Companies)
print ('1. Investor Companies')
print (' ')
with open('stops_Investor_Companies_cln.txt','r') as fin:
print(fin.read())
print (' ')
The result is that the text 1. Investor Companies prints to the screen but the file stops_Investor_Companies_cln.txt is not printed to the screen.
However, I can print the file stops_Investor_Companies_cln.txt to the screen using same code snippet as a separate script;
with open('stops_Investor_Companies_cln.txt','r') as fin:
print(fin.read())
You are not closing the file before opening it again. Data is flushed in file only after you close it.
Try closing the file before opening it again:-
outfile = open ('stops_Investor_Companies_cln.txt', 'w')
outfile.write(stops_Investor_Companies)
outfile.close()
Or you can use one more with option to open the file so that closing of file is taken care of...
Since you haven't closed the file before opening it again, you're not seeing the new data. What you should do is use another with statement when you open the file for writing so it gets closed properly.
Im trying to print an excel document through a default printer configured from command prompt using PRINT command as shown below:
C:>print c:\printdocs\test1.xls
by typing above and pressing enter a line saying 'C:\test1.xls is currently being printed'
is getting displayed but the excel document is not sent for default printer for printing. Please help me with this, where iam going wrong.
print.exe is designed to print a text file, and nothing else. In fact, if you type print /? from a command prompt, it tells you that in the first line of the text:
C:\>print /?
Prints a text file.
PRINT [/D:device] [[drive:][path]filename[...]]
/D:device Specifies a print device.
So the answer to your question is that you can't print an Excel file via print from the command line, because an .XLS file isn't a text file.
The code is:
import MeCab
m = MeCab.Tagger("-O wakati")
text = raw_input("Enter Japanese here: ")
print m.parse(text)
The problem is that after entering the string into the raw_input it gives an error in IDLE:
Traceback (most recent call last):
File "C:\Users\---\Desktop\---\Python\japanesetest.py", line 5, in <module>
print m.parse(text)
File "C:\Users\---\Desktop\---\Python\lib\site-packages\MeCab.py", line 220...
def parse(self, *args): return _MeCab.Tagger_parse(self, *args)
TypeError: in method 'Tagger_parse', argument 2 of type 'char const *'
If I do this however:
import MeCab
m = MeCab.Tagger("-O wakati")
print m.parse('なるほど、マルコフ辞書のキーはタプルにしたほうがスッキリしますね。')
I get the proper result:
なるほど 、 マルコフ 辞書 の キー は タプル に し た ほう が スッキリ し ます ね 。
Things I have tried are unicode tags at the beginning, writing to a textfile in unicode and parsing the text, and a few other million things. I'm running Python 2.7 and MeCab 0.98. If this can't be answer, even a little light shed on the error would be appreciated.
I am able to run your snippet successfully using Python 2.7 and MeCab 0.98 in both IDLE and IPython command line.
import MeCab
m = MeCab.Tagger("-O wakati")
text = raw_input("Enter Japanese here: ")
Enter Japanese here: 私の車はとても高いです。
print m.parse(text)
私 の 車 は とても 高い です 。
However, when reading from a UTF file I will get errors when trying to parse the text. For those cases I explicitly encode the text to shift-jis. You might try this technique. Below is an example.
rawtext = open("UTF.file", "rb").read()
tagger = MeCab.Tagger()
encoded_text = rawtext.encode('shift-jis', errors='ignore')
print tagger.parse(encoded_text).decode('shift-jis', errors='ignore')
This is my current workaround, and should help people coming across the same issue:
import MeCab
import codecs
write_to = codecs.open("pholder.txt", "w", "utf-8")
text = raw_input("Please insert Japanese text here: ")
write_to.write(text)
write_to.close()
read_from = open('pholder.txt').read()
mecab = MeCab.Tagger("-Owakati")
print mecab.parse(read_from)
The deal-breaker here is adding .read() to the open func. Why? Maybe you can tell me. :/