count the number of pages in pdf file - python

I have a problem when I use this code to count the number of pages in pdf file :
if j[i].rstrip() == "Page" or j[i].rstrip() == "page":
rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL)
data = file("/home/suleiman/Desktop/CVE-2011-2462_36EE5F9C51316E060657AA86D48670E8","rb")
print len(rxcountpages.findall(data))
the error is:
Traceback (most recent call last):
File "pdf_scanner.py", line 89, in
main()
File "pdf_scanner.py", line 72, in main
print len(rxcountpages.findall(data))
TypeError: expected string or buffer
can an one help me with it?

you have to read the contents of the file:
data = open("/home/suleiman/Desktop/CVE-2011-2462_36EE5F9C51316E060657AA86D48670E8","rb").read()

Related

I don't understan why checksum error occurs

I found on Internet pynmea2 library, that used the parse(data, check=False) function, which takes a string containing a NMEA 0183 sentence and returns a NMEASentence object.
I try to write some easy (very easy) code to understand functioning:
import pynmea2
def main():
f = open("file.nmea", "r")
for line in f.readlines():
msg = pynmea2.parse(line)
print(str(msg))
So, I read sentences from a file and passed them to parse function, but an error raise:
Traceback (most recent call last):
File "/home/maestrutti15/PycharmProjects/prova/main.py", line 13, in <module>
main()
File "/home/maestrutti15/PycharmProjects/prova/main.py", line 9, in main
msg = pynmea2.parse(str(line))
File "/home/maestrutti15/PycharmProjects/prova/venv/lib/python3.8/site-packages/pynmea2/nmea.py", line 115, in parse
raise ChecksumError(
pynmea2.nmea.ChecksumError: ('checksum does not match: 17 != 3B', ['121626.10', 'A', '4608.25657', 'N', '01313.38859', 'E', '0.071', '270421', 'A', 'V'])
Can anyone tell me why this errors appears? I don't understand... if I write
msg = pynmea2.parse("$GNRMC,121626.15, ..)
in this way, it prints the result.
Thank you!

Replace Carriage Return (CR) and Carriage Return and Line Feed (CRLF) python

I import a .csv file that looks like:
by using the following code:
filetoread = 'G:/input.csv'
filetowrite = 'G:/output.csv'
# https://stackoverflow.com/questions/17658055/how-can-i-remove-carriage-return-from-a-text-file-with-python/42442131
with open(filetoread, "rb") as inf:
with open(filetowrite, "wb") as fixed:
for line in inf:
# line = line.replace('\r\r\n', 'r\n')
fixed.write(line)
print(line)
Which give the output:
b'\xef\xbb\xbfHeader1;Header2;Header3;Header4;Header5;Header6;Header7;Header8;Header9;Header10;Header11;Header12\r\n'
b';;;1999-01-01;;;01;;;;;;\r\n'
b';;;2000-01-01;;;12;;"NY123456789\r\r\n'
b'";chapter;2020-01-01 00:00:00;PE\r\n'
b';;;2020-01-01;;;45;;"NY123456789\r\r\n'
b'";chapter;1900-01-01 00:00:00;PE\r\n'
b';;;1999-01-01;;;98;;;;;;\r\n'
I have issues to replace \r\r\n to \r\n which I guess I need to do to get my desired output.
The error I get when I try to replace the \r\r\n is:
Traceback (most recent call last):
File "g:/till_format_2.py", line 10, in <module>
line = line.replace('\r\r\n', 'r\n')
TypeError: a bytes-like object is required, not 'str'
My desired output:
What do I need to add or change to the code to achieve my desired output?
As the error message says, supply a bytes object.
line = line.replace(b'\r\r\n', b'\r\n')
To get the desired output
line = line.replace(b'\r\r\n', b'')

Trying to read a data from another file in python

I am trying to make my code read the data from a different file. The data in the file emaillist.txt is written in the following format:
a
b
b
c
s
f
s
Now I am tryin to pick a random email from this file and I am getting an error.
Here is the code {Note: this is a piece of code, I have imported the correct libraries}:
with open('emaillist.txt') as emails:
read_emails = csv.reader(emails, delimiter = '\n')
for every_email in read_emails:
return random.choice(every_email)
and this is the error:
Traceback (most recent call last):
File "codeOffshoreupdated.py", line 56, in <module>
'email': email_random(),
File "codeOffshoreupdated.py", line 12, in email_random
for every_email in read_emails:
ValueError: I/O operation on closed file.
Can you please help me fix it? It will be very helpful. Thanks in advance
This code will return you a random email from the emilas that in the file because in your code is returning the first email from the file since it is the first iteration of for every_email in read_emails:
with open('emaillist.txt') as emails:
read_emails = csv.reader(emails, delimiter = '\n')
return random.choice(list(read_emails))[0]
Indent your for loop, like this:
with open('emaillist.txt') as emails:
read_emails = csv.reader(emails, delimiter = '\n')
for every_email in read_emails:
return random.choice(every_email)

I am trying to decompress a file that I created

My code has a file "filefile.txt" which has a compressed sentence in it. The file is laid out like :
1
2
3
4
5
1
2
6
9
10
11
2
12
12
9
This
is
a
sentence
.
too
!
Yo
yo
bling
The original text that I want to decompress says "!"
My code says:
fo = open("filefile.txt","r")
script = fo.readline()
script2 = fo.readline()
fo.close()
script2 = script2.split()
script = [s.strip("\n") for s in script]
sentencewords = []
while len(script) > 0:
for p in script:
sentencewords.append(enumerate(script2.index(p)))
script.remove(0)
print(sentencewords)
This is the error:
Traceback (most recent call last):
File "F:\code attempts\AT13.py", line 46, in <module>
sentencewords.append(enumerate(script2.index(p)))
ValueError: '1' is not in list
I need sentencewords to contain "This is a sentence. This is too! Yo yo bling bling!"
I have changed it now but it still doesn't work.
sentencewords.append(enumerate(script2.enumerate(p)))
'Traceback (most recent call last):
File "F:\code attempts\AT13.py", line 46, in
sentencewords.append(enumerate(script2.enumerate(p)))
AttributeError: 'list' object has no attribute 'enumerate''
Does anyone know if there is another way round this problem or how to fix my current code?
fo = open("filefile.txt","r")
script = fo.readline()
script2 = fo.readline()
fo.close()
script2 = script2.split()
script = [s.strip("\n") for s in script]
sentencewords = []
indexes = []
for line in fo:
if line.strip().isdigit():
indexes.append(line)
else:
break
words = [line.strip() for line in fo if line.strip()]
while len(script) > 0:
for p in script:
sentencewords.append(words[index-1])
print(sentencewords)
Updated code but I don't know what the I/O thing means in the latest output from python.
Traceback (most recent call last):
File "F:/code attempts/attempt14.py", line 45, in <module>
for line in fo:
ValueError: I/O operation on closed file.
fo.close() has been moved further down the code and now it says
Traceback (most recent call last):
File "F:\code attempts\attempt14.py", line 55, in <module>
sentencewords.append(words[index-1])
MemoryError
Any suggestions on how to fix my code, I'd be grateful for
thanks
Format the text file in a better way and it will be easier to deal with.
1 2 3 4 5 1 2 6 9 10 11 2 12 12 9
This is a sentence. too ! Yo yo bling
Then do this...
script = []
sentencewords = []
with open("filefile.txt", "r") as fo:
for line in fo:
script.append(line.strip("\n").split(" "))
for i in script[0]:
sentencewords.append(script[1][int(i)-1])
print(sentencewords)
Your indices above 10 will give issues though, because you don't have that many words.

reading last line of txt file in python and change it into variable to make calculation

td = 'date of transaction.txt'
tf = 'current balance.txt'
tr = 'transaction record.txt'
for line in open(tf):pass
for line2 in open(td):pass
for line3 in open(tr):pass
print line2,line,line3
"""
so call recall back last record
"""
rd=raw_input('\ndate of transaction: ')
print "Choose a type of transaction to proceed... \n\tA.Withdrawal \n\tB.Deposit \n\tC.Cancel & exit"
slc=raw_input('type of transaction: ')
i=1
while (i>0):
if slc=="A" or slc=="B" or slc=="C":
i=0
else:
i=i+1
slc=raw_input('invalid selection, please choose again...(A/B/C): ')
if slc=="A":
rr=input('\namount of transaction: ')
Line_len = 10 # or however long a line is, since in my example they all looked the same
SEEK_END = 2
file = open(tf, "r")
file.seek(-Line_len, SEEK_END)
a = int(str(file.read(Line_len)).split(" ")[0].strip())
rf=a-rr
f1=open(tf, 'a+')
f1.write('\n'+rf)
f1.close()
d1=open(td, 'a+')
d1.write('\n'+rd)
d1.close
r1=open(tr, 'a+')
r1.write('\n-'+rr)
r1.close
else:
print 'later'
above is my code, the function is to get data(last line) from txt file and read it, get new data and write it to the txt file again by creating new line.
my txt file(current balance.txt) should look like this:
2894.00
2694.00
but when i try to use the last line which is 2694.00 to do calculation(rf=a-rr), it failed returning this error:
Traceback (most recent call last):
File "C:\Python27\acc.py", line 27, in <module>
file.seek(-Line_len, SEEK_END)
IOError: [Errno 22] Invalid argument
else if i use this code:
for line in open(tf):
pass
a = line
rf=a-rr
it return this error:
Traceback (most recent call last):
File "C:\Python27\acc.py", line 27, in <module>
rf=a-rr
TypeError: unsupported operand type(s) for -: 'str' and 'int'
I seriously have no idea why...please help me...
To obtain last line of the file, you can simple do
with open('my_file.txt') as file:
last_line = file.readlines()[-1]
#last_line is a string value pointing to last line, to convert it into float, you can do
number = float(last_line.strip('\n').strip(' '))
The function input is giving you a string. Try doing:
rf=a-float(rr)

Categories

Resources