Hello everyone,
I'm reading the rows from a CSV file. csv.DictReader puts
those rows into dictionaries.
The actual files contain old and new translations of software
strings. The dictionary containing the row data looks like this:
o={'TermID':'4' , 'English':'Syst em Administration' ,
'Polish':'Zarza dzanie systemem'}
I put those dictionaries into the list:
oldl=[x for x in orig] # where orig=csv.DictRe ader(ofile ...
...and then search for matching source terms in two loops:
for o in oldl:
for n in newl:
if n['English'] == o['English']:
...
Now, this works. However, not only this is very un-Pythonic, but also
very inefficient: the complexity is O(n**2), so it scales up very
badly.
What I want to know is if there is some elegant and efficient
way of doing this, i.e. finding all the dictionaries dx_1 ... dx_n,
contained in a list (or a dictionary) dy, where dx_i contains
a specific value. Or possibly just the first dx_1 dictionary.
I HAVE to search for values corresponding to key 'English', since
there are big gaps in both files (i.e. there's a lot of rows
in the old file that do not correspond to the rows in the new
file and vice versa). I don't want to do ugly things like converting
dictionary to a string so I could use string.find() method.
Obviously it does not have to be implemented this way. If
data structures here could be designed in a proper
(Pythonesque ;-) way, great.
I do realize that this resembles doing some operation on
matrixes. But I have never tried doing smth like this in
Python.
#---------- Code follows ---------
import sys
import csv
class excelpoldialect (csv.Dialect):
delimiter=';'
doublequote=Tru e
lineterminator= '\r\n'
quotechar='"'
quoting=0
skipinitialspac e=False
epdialect=excel poldialect()
csv.register_di alect('excelpol ',epdialect)
try:
ofile=open(sys. argv[1],'rb')
except IOError:
print "Old file %s could not be opened" % (sys.argv[1])
sys.exit(1)
try:
tfile=open(sys. argv[2],'rb')
except IOError:
print "New file %s could not be opened" % (sys.argv[2])
sys.exit(1)
titles=csv.read er(ofile, dialect='excelp ol').next()
orig=csv.DictRe ader(ofile, titles, dialect='excelp ol')
transl=csv.Dict Reader(tfile, titles, dialect='excelp ol')
cfile=open('cmp file.csv','wb')
titles.append(' New')
titles.append(' RowChanged')
cm=csv.DictWrit er(cfile,titles , dialect='excelp ol')
cm.writerow(dic t(zip(titles,ti tles)))
print titles
print "-------------"
oldl=[x for x in orig]
newl=[x for x in transl]
all=[]
for o in oldl:
for n in newl:
if n['English'] == o['English']:
if n['Polish'] == o['Polish']:
status=''
else:
status='CHANGED '
combined={'Term ID': o['TermID'], 'English': o['English'],
'Polish': o['Polish'], 'New': n['Polish'], 'RowChanged': status}
cm.writerow(com bined)
all.append(comb ined)
# duplicates
dfile=open('dup es.csv','wb')
dupes=csv.DictW riter(dfile,tit les,dialect='ex celpol')
dupes.writerow( dict(zip(titles ,titles)))
"""for i in xrange(0,len(al l)-2):
for j in xrange(i+1, len(all)-1):
if (all[i]['English']==all[j]['English']) and
all[i]['RowChanged']=='CHANGED':
dupes.writerow( all[i])
dupes.writerow( all[j])"""
cfile.close()
ofile.close()
tfile.close()
dfile.close()
--
Real world is perfectly indifferent to lies that
are the foundation of leftist "thinking".
I'm reading the rows from a CSV file. csv.DictReader puts
those rows into dictionaries.
The actual files contain old and new translations of software
strings. The dictionary containing the row data looks like this:
o={'TermID':'4' , 'English':'Syst em Administration' ,
'Polish':'Zarza dzanie systemem'}
I put those dictionaries into the list:
oldl=[x for x in orig] # where orig=csv.DictRe ader(ofile ...
...and then search for matching source terms in two loops:
for o in oldl:
for n in newl:
if n['English'] == o['English']:
...
Now, this works. However, not only this is very un-Pythonic, but also
very inefficient: the complexity is O(n**2), so it scales up very
badly.
What I want to know is if there is some elegant and efficient
way of doing this, i.e. finding all the dictionaries dx_1 ... dx_n,
contained in a list (or a dictionary) dy, where dx_i contains
a specific value. Or possibly just the first dx_1 dictionary.
I HAVE to search for values corresponding to key 'English', since
there are big gaps in both files (i.e. there's a lot of rows
in the old file that do not correspond to the rows in the new
file and vice versa). I don't want to do ugly things like converting
dictionary to a string so I could use string.find() method.
Obviously it does not have to be implemented this way. If
data structures here could be designed in a proper
(Pythonesque ;-) way, great.
I do realize that this resembles doing some operation on
matrixes. But I have never tried doing smth like this in
Python.
#---------- Code follows ---------
import sys
import csv
class excelpoldialect (csv.Dialect):
delimiter=';'
doublequote=Tru e
lineterminator= '\r\n'
quotechar='"'
quoting=0
skipinitialspac e=False
epdialect=excel poldialect()
csv.register_di alect('excelpol ',epdialect)
try:
ofile=open(sys. argv[1],'rb')
except IOError:
print "Old file %s could not be opened" % (sys.argv[1])
sys.exit(1)
try:
tfile=open(sys. argv[2],'rb')
except IOError:
print "New file %s could not be opened" % (sys.argv[2])
sys.exit(1)
titles=csv.read er(ofile, dialect='excelp ol').next()
orig=csv.DictRe ader(ofile, titles, dialect='excelp ol')
transl=csv.Dict Reader(tfile, titles, dialect='excelp ol')
cfile=open('cmp file.csv','wb')
titles.append(' New')
titles.append(' RowChanged')
cm=csv.DictWrit er(cfile,titles , dialect='excelp ol')
cm.writerow(dic t(zip(titles,ti tles)))
print titles
print "-------------"
oldl=[x for x in orig]
newl=[x for x in transl]
all=[]
for o in oldl:
for n in newl:
if n['English'] == o['English']:
if n['Polish'] == o['Polish']:
status=''
else:
status='CHANGED '
combined={'Term ID': o['TermID'], 'English': o['English'],
'Polish': o['Polish'], 'New': n['Polish'], 'RowChanged': status}
cm.writerow(com bined)
all.append(comb ined)
# duplicates
dfile=open('dup es.csv','wb')
dupes=csv.DictW riter(dfile,tit les,dialect='ex celpol')
dupes.writerow( dict(zip(titles ,titles)))
"""for i in xrange(0,len(al l)-2):
for j in xrange(i+1, len(all)-1):
if (all[i]['English']==all[j]['English']) and
all[i]['RowChanged']=='CHANGED':
dupes.writerow( all[i])
dupes.writerow( all[j])"""
cfile.close()
ofile.close()
tfile.close()
dfile.close()
--
Real world is perfectly indifferent to lies that
are the foundation of leftist "thinking".
Comment