The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How to compare files in python?
Discuss How to compare files in python? in the Python Programming forum on Dev Shed. How to compare files in python? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 21st, 2004, 03:57 AM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
How to compare files in python?
I need to read from files with ***.dat extension and compare them. The result should be a holding file with all the information from the files, but the information musstn't be written twice
e.g
file 1
*****
User Adrian
age 34
money 4e+23
money2 2e+21
file2
*****
User Momba
age 30
money 4e+23
money2 2e+22
money3 3e+12
holdingfile
*******
Users: 2
Averageage: 32
money 8e+46 (sum of money)
money2 4e + 43 (sum of money2)
money3 3e+12 (from file2)
etc...
the problem is, when i read data, which i manage with open() func, i need to convert the data in numbers, because i want to make stattistics(average, etc...).
f = open(all files)
f.readlines()
How can I convert read data into lists or dicts?
Can I use dicts to set the data for example:
money as key and 8e+46 as value and then calculate with them, or is this the wrong way?
please help [and excuse my bad english]!
|

July 21st, 2004, 04:46 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
I'm not sure of a function to convert your data into a dictionary, so that's a problem you'll have to solve (it's not too difficult). however, you can convert a string into a list with the split() function, and optionally provide the character to split the string on. example:
Code:
s = 'hello world'
print s.split() #by default will split on blank spaces, returns output ["hello", "world"]
print s.split("w") #returns ["hello ", "orld"]
so from there you can split your data up. then you can convert the values you will be working with mathematically to integers with the int() function, which just takes the value you wish to convert to an integer as its argument. it will raise a ValueError if you supply it with a value that cannot be cast to an integer, like the string 'hello'.
|

July 21st, 2004, 12:20 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Why not loop over the file names, and then iterate over each line in each file and append the line if it isn't already present  . Something like this...
Code:
#!/usr/bin/env python
paths = ('file1.dat', 'file2.dat', 'file3.dat', 'file4.dat')
lines = []
for path in paths:
for line in file(path):
if line not in lines: lines.append(line)
file('results.dat', 'w').writelines(lines)
That should handle the output file anyway  .
Hope this helps,
Mark.
__________________
programming language development: www.netytan.com – Hula
|

July 22nd, 2004, 03:09 AM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
thanks, but the script has to run over a bundle of files, which has different names, such as:
ciabdervin.dat
e1.dat
qb.dat
so i have to find a way to search for ***.dat files and read them. Nevertheless my problem is to map the data in a dict
|

July 22nd, 2004, 06:52 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Well are the files in the same directory or do you need to search your entire computer? But even then, if you know what files you want to work with ahead of tile all you have to do is change the names in 'paths' to point at these files.
Also, what data do you want to put in the dictionary? Really cant say i understood what you're after from your first post...
You can use the int() function to type-cast the numbers in the string before summing them.
Mark.
|

July 22nd, 2004, 07:55 AM
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
thanks guys, i solved this problem with your help..... 
sorry, but i have sometimes problems to express myself.
greets
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|