How can I use python for file processing

Y

yinglcs

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
 
M

Marc 'BlackJack' Rintsch

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
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Marc said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top