Dictionary not printing?

If you look at my code, I am opening the files in read mode after they are translated from web elements and printed to .txt, I print them and they “print” the price file, the name, link and sales totally fine. nothing else is supposed to be happening in this snippet. Yet at the bottom of the terminal you can see the output for line 141 (making the dict) is empty square brackets???

Also despite doing if match not in links:
it still gives me the home page as the first print??? match should be “item/” for aliexpress/item.
I didn’t write aliexpress, just item.

not sure how to fix this ive tried quite a few scope changes

I tried changing things tot heir own def() scope, after opening converting to a list(variable)
then doing a str(variable).split() on them to make sure the list is split by my specified seperator.
nothing is seeming to work.

Also despite using Zip(), and doing for i, j, k, l, etc. it only prints once.
in my old file I was able to get it to print the words in the dictionary but the code is no different really, but it still did it only once. (at first for the links category it was printing the entire .txt file for links as a single link) I fixed this by doing a complicated split() on it like i stated above.
now it won’t print at all.

All this file does is:

Rip the data, do a encode on the name alone (it will give errors if the title has unicode), add everything to their own lists and then print to the files. Then it opens the files again, prints (which you can see it prints) and then won’t make the dict??? also like I said the match in linksfile function not sorting out the "aliexpress us /item/ links.

I’m just a beginner in Python, but my thoughts on this are:

After you openned and printed the files by print(files.read()), the file object’s position went to the end of the file.
Then your subsequent operations on that file object were always at that position, which is empty.

E. g., after these codes:

priceagain = open("Prices1.txt", "r")
print(priceagain.read())

the position of the file object ‘priceagain’ will go to the end of that file. Then when you try to convert that file object to str, and assign it to a variable by priceagainstr = list(priceagain), it’s actually empty value assigned to that variable.

I think what you should do is to add a line priceagain.seek(0) after the print lines. That will reset the position of the file object to the beginning of that file.

P. s., as I’ve just learnt from other members, it seems copy-and-paste your code in between of ''', in addition to screenshot, is more welcomed in this community :slightly_smiling_face:

Easier is to assign the result of read() to a variable, then after printing it you still have it available, e.g.

with open("Prices1.txt", "r") as priceagain:
    prices1data =  priceagain.read()

print(proces1data)

... do something else with priceagain_data ...

Note that I used a context manager here for opening the file, this saves you from having to remember to close the file (the CM will do that for you)

1 Like

Please copy and paste your script using the back ticks ` found in the key just to the left of the number 1 of the keyboard. User three back ticks in series - each on their own line - both above and below the pasted code).

— First —

Add these two lines so that files created during testing will be placed on the desktop for easy reference:

import os
os.chdir(r'C:\Desktop')

This line:

nameagain = open("Names1.txt", "r")

Implies that the contents are of type str since the file is of type .txt.

A text file is not allowed to have type binary (bytes) literals. However, when you execute this line:

print(nameagain.read())

You don’t get an error. This means that all of its contents are of type str. In your
printout below, it implies type binary per the b' prefix, per this (only showing partial for brevity):

[b'LEGO 42164 Technic Racing Buggy Off Road Rally Vehicle Toy Race Car Building Set, ...']

However, we know that it cannot be type binary since it is a text file and text files can’t have byte literals. This means that the information is actually all of type string str. We also know this because you cannot write a list to a text file.

For it to have been a binary file, you would have had this instead:

nameagain = open("Names1.bin", "rb")  # You don't have it like this, thus not binary type file

To test this, try this (then try again by removing the leading and ending double quotes – with the double quotes in place, it is all a string. Without the double quotes, it is a list with byte string literals as elements - you should get an exception error when removing them and retrying):

# Create a dummy string of type str that appears as type bytes when read and printed out.

strings_to_write = "[b'Today is Thursday!', b'Time to begin scripting Python', b'We are in the month of April', b'Looking forward to summer.']"
print(type(strings_to_write))

with open("handle4.txt", "w") as names_again:
    names_again.write(strings_to_write)

with open("handle4.txt", "r") as names_again:
    print(names_again.read())

— Second —

This here:

nameagainstr = list(nameagain)  

Does not make sense since it is the handle of the list object created and NOT the actual contents
as per your implied printout at the bottom of your post. This is a bit misleading. Because then
you pass it to:

nameagainstr = list(nameagain)  # You're creating a list of the object handle and not its strings

What you are looking for is instead:

with open('Names1.txt, 'r') as f:
    namesagain = f.read()

or more concisely:

with open('Names1.txt, 'r') as f:
    namesagain = f.read().split(',')

This applies to your other files as well. I have only highlighted this for one file for simplicity.

Test these snippets. At every turn, make sure you use the print statements to verify that your
script is progressing as expected.

This:

nameagain = open("Names1.txt", "r")

implies that the contents are of type str because the mode is "r", which tells Python to open it as a text file.

Reading from a text file returns str.

If the mode is "rb", the b is telling Python that you want to open it as a binary file.

Reading from a binary file will return bytes.

The file’s extension is irrelevant as far as Python is concerned.

You can open any file as a binary file without a problem, but if you open a file as a text file, it’ll complain with an exception if it sees a sequence of bytes that don’t make sense in the expected encoding.

I have fixed all these problems but simply opening them in “r” mode and appending them to a file.txt, with strip(""). This prints them in a ladder and makes them much easier to handle.
Now the only problem I have is that when I encode the selenium web element object, I can’t decode it or it somehow becomes a web object element or unreadable again if it contains unicode characters

so “b’string\12983813x random string” is the encoded version, reads fine.
if I decode it back, no longer readable, or it literally converts back into a web element even if I append to a file first as if it were retaining some data that makes it a web element behind the scenes?
the only thing I have found that works is doing a str(ascii()).split() on it, which sometimes works and removes the binary from ascii()? then I guess it turns it into a str and then into a list.

From your other post, you stated that you are using Notepad++. Can you instead try using a direct Python IDE? Such as IDLE, PyCharm, or equivalent. When I tested your string, it works just fine.

When I tested the script using PyCharm, this is what I observed:

raw_bytes = b'string\12983813x random string'
str_bytes = raw_bytes.decode(encoding='utf-8')

the output was:

string
983813x random string

Screenshot:

Not sure if using Notepad++ is the source of the issue. Like I stated above, I would test the script using a Python IDE instead just to remove any extraneous unknowns.

Okay so I actually fixed this issue, apparently it had something to do with selenium running at 2x speed, while the rest of the script within the same function was running at 1x speed. some backend memory management/threading im sure.
I used with open() for all my files and it instantly went away, I think that the normal open() and close() methods just could not keep up with a function looping over itself multiple times and would times print, sometimes not.

it may also have been in my num = [1, 2, 3, 4] i was using to iterate through the dictionary, I changed it to num = list(range(1, 60 + 1)) and it works fine now.