patternpythonMinor
Formatting inventory of parts
Viewed 0 times
formattinginventoryparts
Problem
I have one file with indexes. For example, they are:
And a dictionary, because I want to match entries that I choose:
Since they are on the same file, my program scans for the
It's a very large file with a lot of numbers.
Would there be a way to make it so it only scans it once? Or any other ideas to put better this program?
There is a text file formatted this way:
In real life, it would have thousands of entries.
Then, my Python file 'knows' that the part number 4719 matches the part number 4720c01 via the
Now, what I think does:
`invFormatted = open('export.txt', 'r') #Opens the file with the part numbers
with open ('upload.txt', 'a') as upload:
upload.write("") #Something that would be on the export
for aline in invFormatted:
lines = aline.split("\t") #Splits the text file on every tab
lotID = lines[0] #The lot ID is the first word
partNO = lines[1] #Part number the second one
if partNO in similar: #Sees if the part number in question has a match, if YES
for alines in invFormatted: #Searchs my inventory file again to see if the match is in my inventory
1 Fruit
2 Meat
3 Fish
4 Salmon
5 Pork
6 Apple
And a dictionary, because I want to match entries that I choose:
similar = {'1' : '6', '2' : '5'}
Since they are on the same file, my program scans for the
'1' on the file, and then re-scans from the beginning looking for the '6'. Same with all the numbers, always re-scanning.It's a very large file with a lot of numbers.
similar = { '4719' : '4720c01' }
for aline in invFormatted:
lines = aline.split("\t") #Splits the CSV file on every tab.
lotID = lines[0]
partNO = lines[1] #Splits and gets the part that would be the "index"
if similar.has_key(partNO):
for alines in invFormatted:
liness = alines.split("\t")
lotIDmatch = liness[0]
partNOmatch = liness[1]
if liness[1] == similar[partNO]:
Would there be a way to make it so it only scans it once? Or any other ideas to put better this program?
There is a text file formatted this way:
51689299 4719 Medium Azure Riding Cycle
51689345 4720c01 Trans-Clear Wheel & Tire Assembly
In real life, it would have thousands of entries.
Then, my Python file 'knows' that the part number 4719 matches the part number 4720c01 via the
similar dictionary:similar = { '4719' : '4720c01' }
Now, what I think does:
`invFormatted = open('export.txt', 'r') #Opens the file with the part numbers
with open ('upload.txt', 'a') as upload:
upload.write("") #Something that would be on the export
for aline in invFormatted:
lines = aline.split("\t") #Splits the text file on every tab
lotID = lines[0] #The lot ID is the first word
partNO = lines[1] #Part number the second one
if partNO in similar: #Sees if the part number in question has a match, if YES
for alines in invFormatted: #Searchs my inventory file again to see if the match is in my inventory
Solution
Can you store the content in the CSV file in the memory?It will be more effective to read from the memory than the disk.
Maybe the CSV file will also be store in the memory cache by OS, but you can do it your self to make it more reliable.
Of course it's only when there is enough memory you can do this.
OK, from your code, I think maybe you just need to read all line[1] in and then scan it.
This assume that: what you write to file 'upload' will not match similar keys and values. Because it read the file and use the 'cache' before write into it.
Maybe the CSV file will also be store in the memory cache by OS, but you can do it your self to make it more reliable.
Of course it's only when there is enough memory you can do this.
OK, from your code, I think maybe you just need to read all line[1] in and then scan it.
partNOs=[line.split("\t")[1] for line in upload]
for partNO in partNOs:
if partNO in similar and similar[partNO] in partNOs:
upload.write("blabla")This assume that: what you write to file 'upload' will not match similar keys and values. Because it read the file and use the 'cache' before write into it.
Code Snippets
partNOs=[line.split("\t")[1] for line in upload]
for partNO in partNOs:
if partNO in similar and similar[partNO] in partNOs:
upload.write("blabla")Context
StackExchange Code Review Q#39292, answer score: 2
Revisions (0)
No revisions yet.