473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I use python for file processing

Hi,

I am trying to use python for file processing.
Suppose I have a file like this:
I want to build a Hashmap between the line "begin_QOS_statistics" and
"end_QOS_statistics"
and for each line I want to put the first text as the key of the hash
table and the second text as the value.

Received RTCP "BYE" on "audio/MPA" subsession (after 89 seconds)
Received RTCP "BYE" on "video/MP4V-ES" subsession (after 89 seconds)
begin_QOS_statistics
server_availability 100
stream_availability 100
subsession video/MP4V-ES
num_packets_received 3529
num_packets_lost 0
elapsed_measurement_time 82.302305
kBytes_received_total 3756.422000
measurement_sampling_interval_ms 100
kbits_per_second_min 0.000000
kbits_per_second_ave 365.134075
kbits_per_second_max 2634.760139
packet_loss_percentage_min 0.000000
packet_loss_percentage_ave 0.000000
packet_loss_percentage_max 0.000000
inter_packet_gap_ms_min 2147483.647000
inter_packet_gap_ms_ave 0.000000
inter_packet_gap_ms_max 0.000000
subsession audio/MPA
num_packets_received 1414
num_packets_lost 0
elapsed_measurement_time 82.302305
kBytes_received_total 1187.644000
measurement_sampling_interval_ms 100
kbits_per_second_min 0.000000
kbits_per_second_ave 115.442113
kbits_per_second_max 1132.007640
packet_loss_percentage_min 0.000000
packet_loss_percentage_ave 0.000000
packet_loss_percentage_max 0.000000
inter_packet_gap_ms_min 0.022000
inter_packet_gap_ms_ave 52.214517
inter_packet_gap_ms_max 140.955000
end_QOS_statistics

Aug 26 '07 #1
2 1399
On Sun, 26 Aug 2007 06:05:11 +0000, yi*****@gmail.com wrote:
I am trying to use python for file processing.
Suppose I have a file like this:
I want to build a Hashmap between the line "begin_QOS_statistics" and
"end_QOS_statistics"
and for each line I want to put the first text as the key of the hash
table and the second text as the value.
Work through the tutorial, experiment a little in the interactive
interpreter and then just do it. You need to `open()` the file, iterate
over its lines in a ``for``-loop, decide to when it is time to start
processing lines, and then `str.split()` the lines and put them into a
`dict()`. This is a neat little project to start learning the language.

Ciao,
Marc 'BlackJack' Rintsch
Aug 26 '07 #2
Marc 'BlackJack' Rintsch wrote:
On Sun, 26 Aug 2007 06:05:11 +0000, yi*****@gmail.com wrote:
>I am trying to use python for file processing.
Suppose I have a file like this:
I want to build a Hashmap between the line "begin_QOS_statistics" and
"end_QOS_statistics"
and for each line I want to put the first text as the key of the hash
table and the second text as the value.

Work through the tutorial, experiment a little in the interactive
interpreter and then just do it. You need to `open()` the file, iterate
over its lines in a ``for``-loop, decide to when it is time to start
processing lines, and then `str.split()` the lines and put them into a
`dict()`. This is a neat little project to start learning the language.

Ciao,
Marc 'BlackJack' Rintsch
Just posted this on Python Tutor, might give you a start :

nameFile = open(r'/path/to/file.txt', 'rU')
phonebook = {}

for line in nameFile :
phonebook.setdefault(line[0].upper(), []).append(line.strip('\n'))

for item, names in phonebook.iteritems() :
names.sort()

print phonebook

HTH

Aug 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christian Long | last post by:
Hi I'm trying to pipe data into a python program on Windows 2000, on the command line. Like this: dir | myProgram.py Here's what I tried:
49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
28
by: Jay | last post by:
OK, I have this XML doc, i dont know much about XML, but what i want to do is take certain parts of the XML doc, such as </title> blah </title> and take just that and put onto a text doc. Then...
0
by: | last post by:
Greetings. In an effort to get python2.4 on my Centos 3.7, I installed the python bootstrap rpm. This installed 2.4 alongside 2.2 and updated yum to 2.4.0. Oddly, it didn't create a symlink...
0
by: Nobody | last post by:
I have an application that processes MIME messages. It reads a message from a file, looks for a text/html and text/plain parts in it, performs some processing on these parts, and outputs the new...
9
by: durumdara | last post by:
Hi! Where can I ask it? I want to ask that developers change the Python's Zip lib in the next versions. The Zip lib not have a callback procedure. When I zip something, I don't know, what is...
4
by: mlimber | last post by:
I'm writing a text processing program to process some survey results. I'm familiar with C++ and could write it in that, but I thought I'd try out Python. I've got a handle on the file I/O and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.