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

Reply
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 October 4th, 2012, 10:48 AM
vivekd vivekd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 vivekd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 28 sec
Reputation Power: 0
Need help

I have something like this .....

10280341|2012-10-03 19:11:06.390|Sami|abc|Crossword|70
10280343|2012-10-03 19:15:32.173|Sami|aaa|Sudoku|30
10280355|2012-10-04 19:15:32.173|miami|bbb|Chaircar|15
10280366|2012-10-04 19:15:32.173|miami|bob|Avista|35

And i want an OUTPUT like this grouped by name and date .....


2012-10-03
Sami|2|100

2012-10-04
miami|2|50

Reply With Quote
  #2  
Old October 4th, 2012, 04:34 PM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
text file

It would probably help your chances at responses if you were a little more personable.

for opening your text and output file, i would use:

Code:
with open(filename, mode='r') as input_stream:
  Data = input_stream.readlines()
  SData = Data.strip()
with open(filename, mode='w') as output_stream:


then find the pipes, and pull out the data between them:

Code:
for Line in SData :
  for Cnt1 in range(len(Line)) :
    if '|' in SData[Cnt1] : 
      Pipe1 = Cnt1
      break

  for Cnt2 in range(len(Line[Cnt1:-1]))
    if '|' in Line[Cnt2] : 
      Pipe2 = Cnt2
.     break
.
.
.
  for Cnt5 in range(len(Line[Cnt4:-1]))
    if '|' in Line[Cnt5] :
      Pipe5 = Cnt5
      break

for Line in SData :
  output_stream.write(Line[Cnt1+1:Cnt1+9])
  output_stream.write(Line[Cnt2+1:Cnt3]+'whatever else that is'+'\n'

i wasn't able to test this, but the general idea should be obtainable.


Reply With Quote
  #3  
Old October 4th, 2012, 07:22 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Quote:
Originally Posted by WynnDeezl
Code:
for Line in SData :
  for Cnt1 in range(len(Line)) :
    if '|' in SData[Cnt1] : 
      Pipe1 = Cnt1
      break

  for Cnt2 in range(len(Line[Cnt1:-1]))
    if '|' in Line[Cnt2] : 
      Pipe2 = Cnt2
.     break
.
.
.
  for Cnt5 in range(len(Line[Cnt4:-1]))
    if '|' in Line[Cnt5] :
      Pipe5 = Cnt5
      break

for Line in SData :
  output_stream.write(Line[Cnt1+1:Cnt1+9])
  output_stream.write(Line[Cnt2+1:Cnt3]+'whatever else that is'+'\n'
There's a much easier way to do all this:
python Code:
Original - python Code
  1. # For compatibility with Python 2.x
  2. from __future__ import print_function
  3.  
  4. with open(input_filename) as input_file, open(output_filename, 'w') as output_file:
  5.     for line in input_file:
  6.         fields = line.strip().split('|')
  7.  
  8.         name = fields[2]
  9.         (date, time) = fields[1].split(' ')
  10.  
  11.         print(date, file=output_file)
  12.         # etc.
  13.  

Reply With Quote
  #4  
Old October 4th, 2012, 10:17 PM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
nice

that is good compact code, but it looks like its only writing the date, not the rest of the data required

Reply With Quote
  #5  
Old October 4th, 2012, 11:12 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Quote:
Originally Posted by WynnDeezl
that is good compact code, but it looks like its only writing the date, not the rest of the data required
Obviously, hence the "#etc." at the end. I assume the OP can figure out what's going or ask further questions if there's still trouble.

Reply With Quote
  #6  
Old October 6th, 2012, 01:54 AM
vivekd vivekd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 vivekd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 28 sec
Reputation Power: 0
Need To Group It

[QUOTE=Hey thanks a lot for your suggestions but having difficulties in grouping it. As i am new to python i need help regarding this output.[/QUOTE]

Grouping by DATE,
Grouping by NAME|Len(NAME)|Sum(Values) i.e

2012-10-03
Sami|2|100
2012-10-04
miami|2|50
2012-10-05
Sami|1|20

Reply With Quote
  #7  
Old October 6th, 2012, 06:03 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,386 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 5 m 35 sec
Reputation Power: 383
from
10280341|2012-10-03 19:11:06.390|Sami|abc|Crossword|70
10280343|2012-10-03 19:15:32.173|Sami|aaa|Sudoku|30

You'd like this report:
2012-10-03
Sami|2|100

There are 2 Sami entries on 2012-10-03, and the sum of the last field equals 100.

This accounts for the field values.

Next, instead of doing "the simplest thing that could possibly work", I assume you chose over-simplified input.

Code:
garbage_in = '''
    10280341|2012-10-03 19:11:06.390|Sami|abc|Crossword|70
    10280355|2012-10-03 19:15:32.173|miami|bbb|Chaircar|15
    10280343|2012-10-03 19:15:32.173|Sami|aaa|Sudoku|30
    xxxxxxxx|2012-10-04 19:15:32.173|Salami|aaa|Sudoku|3
    10280355|2012-10-04 19:15:32.173|miami|bbb|Chaircar|1
    10280366|2012-10-04 19:15:32.173|miami|bob|Avista|2
    10280366|2012-10-04 19:15:32.173|miami|bob|Avista|3
    10280366|2012-10-04 19:15:32.173|miami|bob|Avista|4
'''

d = {}

for line in garbage_in.split('\n'):
    fields = line.split('|')
    if 6 == len(fields):
        date = fields[1].split()[0]
        name = fields[2]
        key = (date,name)
        if key not in d:
            d[key] = [0,0]
        a = d[key]
        a[0] += 1               # occurrence count
        a[1] += int(fields[-1]) # conversion to integer, sum of last field

L = []
for key in sorted(d):
    (count,Sum) = d[key]
    L.extend(['',key[0],'%s|%s|%s'%(key[1],count,Sum)])

garbage_out = '\n'.join(L)

print(garbage_out)




'''

2012-10-03
Sami|2|100

2012-10-03
miami|1|15

2012-10-04
Salami|1|3

2012-10-04
miami|4|10
'''
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help

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