import json
# Load JSON data from a file
with open('data.json', 'r') as file:
data = json.load(file)
# Function to check if an element exists in the JSON data
def check_element_exists(json_data, element):
return element in json_data['data']
# Example usage
element_to_check = 'element100000000'
exists = check_element_exists(data, element_to_check)
# Print True or False
print(exists)
#π Any faster approach to check if text exists in file, there can be around 100 Million elements in j
75 messages Β· Page 1 of 1 (latest)
@sick bison
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
what size is your json (in disk space ?)
well, unless you've got an index to that data, I doubt there's a faster way
cause if you're running into ram issues, whether you open it as a file or as an object won't change much
what, specifically, is this data, and why are you checking it?
around 120 GB
a 120GB JSON file sure doesn't seem useful
user query
the correct thing to do would be to convert your entire thing to something that isn't json, like mysql
json is not a database format
They might be better off with a nosql solution depending on what days they have tbf
I have a section in my app where I will show different pages to user, each page has title. and title of all the page are stored in json file. I need to check If I have showed the page to user, if not I will show that to user. In really there will be max 30 million elements(titles) in json file but I am testing for 1 billion. It will never cross that 30 million mark
why do you need to store anything other than pairs of users & titles?
Just go through that list to see if you've already displayed it. If not, display it and add it to that list.
I doubt that list will have zillions of elements.
max will be 30 million. User can't see more than 30 million pages in his lifetime
Yes wanted to do that but in the best way possible
but may be that json file will be required in future so easily it can uploaded to cloud
well I don't understand, but whatever
You understand this, right?
JSON is simply not suited to the operation that you want
It's clear what you're trying to do, it's not clear why you're doing it in that particular way, so it's hard to help imo
have a habit of using json only for all things but mysql also works
well JSON has limits you can't just use it "for all things"
so I should store in .db file
yes, I am very well aware
you're starting to use huge amounts of data so you'll want some database
and that's not just a size issue but a performance and data availability issue overall for your users too
I don't know the structure of your data, it's impossible to know what to recommend
But some options could be mongodb or mysql, but you should think about why you're choosing a particular option
I understand that very welll
just a list
nothing else
if you tell us more about what's stored in that json, we could give you pointers towards which database to choose
a list of what ?
and those operations need to be done at which frequency ?
['Mukesh Ambani','the wolf of the wall street',' baseball bat', 'shark tank', 'Trump']
list will be like this it can be any entity
is it just a list of strings ?
need to check around 5 times if text in file in 1 seconds(5 different threads)
yes
and you want fast access to those ?
cause if so i guess a key value database like Redis would work just fine
only read to check if text exists
and write to write a title which is visited
yes
You need to do this per user? How many users are there?
It is done locally so it doesn't matter, right?
For now I don't know how many users will use the app but at least a million
Each user will locally store your 120GB data file?
wdym by "it is done locally" ?
I said I was just checking it. The file size will never cross 20mb
maybe you should start from the beginning and explain fully what's your usecase so we can better understand
this feels very much so like a XY problem
^
I was just checking on 1 billion elements actually there will be max 30 million elements
sure
I have a list like this ['Mukesh Ambani','the wolf of the wall street',' baseball bat', 'shark tank', 'Trump'] which is consists of 30 million elements. I need to check If a new element is present in a list. If not, Need to add that in list
The operation of check will be performed 5 times per sec using threads
so to check that I am using
import json
# Load JSON data from a file
with open('data.json', 'r') as file:
data = json.load(file)
# Function to check if an element exists in the JSON data
def check_element_exists(json_data, element):
return element in json_data['data']
# Example usage
element_to_check = 'element100000000'
exists = check_element_exists(data, element_to_check)
# Print True or False
print(exists)
string can be of any human language . Need to check that locally (file will be in device storage and file size will be 30 mb). The app is already taking ram of around 700mb. So need a method which will consume less ram
Is there any more context you can add?
not really, The app is made for Android, ios, mac os, windows and some operating systems of T.V
I will try
I have edited with atmost details
But what are you actually trying to do
Like what is the business problem you're trying to solve
just showing doc/page which is not shown to user. user needs to see all pages of all docs
@sick bison check mmap ( memory mapping ) in python, it's a module specially used for reading large files which cannot be directly loaded into RAM
I had the same issue with log files of more than 10 GB, which i solved with using mmap
that's not json
json has no single quoted strings.
also apparently your json file contains an object with a "data" key, not a list.
if the file had a specific format (e.g. a json list of strings, with line breaks between the items) you could search the title line-by-line without having to parse the whole file.
additionally, if the list was sorted you could use binary search.
{"data": [
"baseball bat",
"Mukesh Ambani",
"shark tank",
"the wolf of the wall street",
"Trump",
]}```
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.