Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 21st, 2004, 03:57 AM
MQ-Sephiroth MQ-Sephiroth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 MQ-Sephiroth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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]!

Reply With Quote
  #2  
Old July 21st, 2004, 04:46 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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'.

Reply With Quote
  #3  
Old July 21st, 2004, 12:20 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #4  
Old July 22nd, 2004, 03:09 AM
MQ-Sephiroth MQ-Sephiroth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 MQ-Sephiroth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old July 22nd, 2004, 06:52 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old July 22nd, 2004, 07:55 AM
MQ-Sephiroth MQ-Sephiroth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 MQ-Sephiroth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to compare files in python?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap