I've uploaded a PDF using PyPDF2. Here's my code:
PageNo=list(range(0,pfr.numPages))
for i in PageNo:
pg = writer.addPage(i)
PageNo creates a list of all the page numbers and I'm trying to add each page from the list into a new PDF, but when I run this script, I'm getting 'int' object is not subscriptable. I know it's an error in my for loop, and I'm sure it's obvious, but any insight on my mistake above would be appreciated.
Thanks!
Edit - here's the solution:
for i in list(range(0,pfr.numPages))
pg = pfr.getPage(i)
writer.addPage(pg)
Without seeing more code, like what exactly is pfr.numPages? Where is it created? How is it used? the error is happening because PageNo is being evaluated as an int, so its less likely its an error in your for loop and more likely an error in your list. Assuming pfr.numPages is an array of some type, try this:
for i in range(0, len(pfr.numPages)):
pg = writer.addPage(prf.numPages[i])
However as mentioned before, if PageNo is being evaluated as an int, then pfr.numPages isn't an array or its an array with one element. Pay close attention to the trace if this errors, it will tell you which line, and if it says int is not subscriptable on the line with prf.numPages, then you know that your problem is deeper than this for loop.
I am assuming that writer is a PdfFileWriter object. In that case, you will have to pass a page retrieved from pfr to the writer.addPage method, instead of passing an integer.
for i in pfr.pages:
writer.addPage(i)
Related
I am trying to take the value from the input and put it into the browser.find_elements_by_xpath("//div[#class='v1Nh3 kIKUG _bz0w']") function. However, the string formatting surely doesn't work, since it's the list, hence it throws the AttributeError.
Does anyone know any alternatives to use with lists (possibly without iterating over each file)?
xpath_to_links = input('Enter the xpath to links: ')
posts = browser.find_elements_by_xpath("//div[#class='{}']").format(devops)
AttributeError: 'list' object has no attribute 'format'
Looks like the reason of error is that you are placing the format function in the wrong place, so instead of operating on string "//div[#class='{}']" you call it for the list returned by find_elements_by_xpath. Could you please try to replace your code with one of the following lines ?
posts = browser.find_elements_by_xpath("//div[#class='{}']".format(devops))
posts = browser.find_elements_by_xpath(f"//div[#class='{devops}']")
Hi so I loaded a JSON file into a list using the following code:
import json
postal_mtl = ['H9W','H4W','H9P','H3B','H3A','H2Z','H3H','H3G','H3X','H9J','H1B','H1G','H1H','H4X','H2Y','H9R','H3Z','H3Y']
data = []
with open('business.json',encoding="utf8") as f:
for line in f:
data.append(json.loads(line))
Now I am trying to find the number of restaurants in montreal in this dataset (coming from Yelp). I tried the following code:
compteur3 = 0
for i in range(len(data)):
if data[i]['postal_code'][0:3] in postal_mtl and 'Restaurants' in data[i]['categories']:
compteur3 += 1
print(compteur3)
But I am getting an error saying "argument of type 'NoneType' is not iterable" I guess Python considers the date[i]['categories'] as a Nonetype ? Why is that ? If I enter the following I can see that it's clearly a string:
data[5]['categories']
'Shipping Centers, Couriers & Delivery Services, Local Services, Printing Services'
Now I just want to iterate over all the elements in my data list and find each line where we have the word 'Restaurants' (I got the Montreal stuff fixed)... Any idea ? Thanks !
Based on the code provided, it seems that the error is most likely coming from the if condition. Specifically, it is most likely coming from the statement 'Restaurants' in data[i]['categories']. Under the hood, Python is trying to iterate through data[i]['categories'] to see if 'Restaurants' is in it. If data[i]['categories'] is None, that would cause this error.
This may be caused by the JSON string not being formatted the way you expected. Perhaps, if no categories were listed in the 'Categories' field, a null was put instead of an empty list. To check for this in your code, you can try the following:
compteur3 = 0
for i in range(len(data)):
is_inmontreal = data[i]['postal_code'][0:3] in postal_mtl
is_restaurant = data[i]['categories'] and 'Restaurants' in data[i]['categories']
if is_inmontreal and is_restaurant:
compteur3 += 1
print(compteur3)
Above, I simply split the condition into two parts. Functionally, this would be the same as having the conditions in one line, it just makes it slightly clearer. However, I also added a check in is_restaurant to see if data[i]['categories'] has a positive truth value. In effect, this will check if the value is not None and it is not an empty list. If you really want to be explicit, you can also do
is_restaurant = data[i]['categories'] is not None and 'Restaurants' in data[i]['categories']
Depending on how dirty the data is, you may need to go a little further than this and use exception handling. However, the above is just speculation as I do not know what the data looks like.
i want to extract 'src' from 'img' tag
I have the code
lists = soup.find(class_="image-container image__pic js-image-pic")
for img in lists:
image = img.find('img')['src']
print (image)
And i got this error 'int' object is not subscriptable
I imagine there is other useful information in the error (you should always put a full error trace), but without this I will say you are likely using .find() when you mean to use .find_all(). One will return a list, and the other will return a single item. I imagine (again, speculating since there is no trace posted) that if you ran:
lists = soup.find_all(class_="image-container image__pic js-image-pic")
for img in lists:
image = img.find('img')['src']
print(image)
It would work as expected. When I have encountered this error that has been the solution for me at least
Warning: I'm a total newbie; apologies if I didn't search for the right thing before submitting this question. I found lots on how to ignore errors, but nothing quite like what I'm trying to do here.
I have a simple script that I'm using to grab data off a database, parse some fields apart, and re-write the parsed values back to the database. Multiple users are submitting to the database according to a delimited template, but there is some degree of non-compliance, meaning sometimes the string won't contain all/any delimiters. My script needs to be able to handle those instances by throwing them out entirely.
I'm having trouble throwing out non-compliant strings, rather than just ignoring the errors they raise. When I've tried try-except-pass, I've ended up getting errors when my script attempts to append parsed values into the array I'm ultimately writing back to the db.
Originally, my script said:
def parse_comments(comments):
parts = comments.split("||")
if len(parts) < 20:
raise ValueError("Comment didn't have enough || delimiters")
return Result._make([parts[i].strip() for i in xrange(2, 21, 3)])
Fully compliant uploads would append Result to an array and write back to db.
I've tried try/except:
def parse_comments(comments):
parts = comments.split("||")
try:
Thing._make([parts[i].strip() for i in xrange(2, 21, 3)])
except:
pass
return Thing
But I end up getting an error when I try and append the parsed values to an array -- specifically TypeError: 'type' object has no attribute 'getitem'
I've also tried:
def parse_comments(comments):
parts = comments.split("||")
if len(parts) >= 20:
Thing._make([parts[i].strip() for i in xrange(2, 21, 3)])
else:
pass
return Thing
but to no avail.
tl;dr: I need to parse stuff and append parsed items. If a string can't be parsed how I want it, I want my code to ignore that string entirely and move on.
But I end up getting an error when I try and append the parsed values to an array -- specifically TypeError: 'type' object has no attribute 'getitem'
Because Thing means the Thing class itself, not an instance of that class.
You need to think more clearly about what you want to return when the data is invalid. It may be the case that you can't return anything directly usable here, so that the calling code has to explicitly check.
I am not sure I understand everything you want to do. But I think you are not catching the error at the right place. You said yourself that it arose when you wanted to append the value to an array. So maybe you should do:
try:
# append the parsed values to an array
except TypeError:
pass
You should give the exception type to catch after except, otherwise it will catch any exception, even a user's CTRL+C which raise a KeyboardInterrupt.
I made this code in python but it has much errors that i want you to check it for me as I'm new at python
for i in range(phones):
pho = int(raw_input( "Phone Number %d : " % (i+1)))
phNums.append(pho)
for name in range(phot):
name1 = '{0}/phone.txt'.format(pathname)
file = open(name1, 'w')
file.write = (pho)
file.close()
First issue that when i run the script i had this error
Traceback (most recent call last):
file.write = (pho)
AttributeError: 'file' object attribute 'write' is read-only
The script should do this scenario :
First the user give the number of Perfixes and the count of the progs
The script will count phot which it the progs / phones
then the user will give each phone number
the script will take the first number and write it in the text file "phone.txt" on the folders 1 2 3 until reach phot count of folders then move to the next number and continue writing to next count of folders ... etc
please check what is the issue with the code
phNums.append(pho) adds ints to your list phNums, you then try to iterate over phNums[i] which is each int in your list which you cannot do and why you get the error.
Just iterate over each element directly:
for i in phNums:
name = '{}phone.txt'.format(i) # i is an int so use str.format to concat
Or looking at your code, you may have meant to iterate over a list of names that is not provided in your code.
This is a simple error that has already been answered sufficiently, so rather than give you a direct answer, I want to direct you through my debugging process. Hopefully this will save time in the future.
First step is to check where the error occurred:
for name in phNums[i]:
Ok, now what was the error?
TypeError: 'int' object is not iterable
The "for" statement will iterate over a given iterable and is generally like this:
for _item_ in _iterable_
Now we know which part of the line to look at. What type is phNums[i] ?
phNums is defined earlier as a list, but with phNums[i], we want to find the type of the items in that list. It appears that the only time the script adds to the list is when appending pho, which is an int.
There's the issue, you're trying to iterate over an int item! The error makes sense now, but how should we fix it?
note: I tried going further, but your description is unclear. Do you want the folder structure to be:
1/phone.txt
2/phone.txt
3/phone.txt
where phone.txt contains the phone number in each?