Quck question

J

J

I have the following information in a file :
r1kcch Serial0/0/0 propPointToPointSerial
Mon Jan 5 13:15:03 PST 2004 InOctets.1 0
Mon Jan 5 13:15:05 PST 2004 OutOctets.1 0

I want to be able to extract each line into a comma delimited list.
Bellow i am trying to print out my list print currentList but nothing
is comming out. I added a line after print test to make sure the file
has the information. Can someone help me with this?


#Read a file
true = 1
in_file = open("test.txt","r")
text = in_file.read()
while true:
in_line = in_file.readline()
if in_line == "":
break
currentList = string.split(in_line,",")
#print out the contents of the current list
print currentList
#make the inline stop reading
in_line = in_line[:-1]
in_file.close()
 
T

Tim Heaney

I have the following information in a file :
r1kcch Serial0/0/0 propPointToPointSerial
Mon Jan 5 13:15:03 PST 2004 InOctets.1 0
Mon Jan 5 13:15:05 PST 2004 OutOctets.1 0

I want to be able to extract each line into a comma delimited list.
Bellow i am trying to print out my list print currentList but nothing
is comming out. I added a line after print test to make sure the file
has the information. Can someone help me with this?

I'm not certain I understand what you're doing, but perhaps you want
something like this?

file = open("test.txt")
for line in file:
list = line.split()
if len(list):
print ','.join(list)

I hope this helps,

Tim
 
E

engsolnom

I have the following information in a file :
r1kcch Serial0/0/0 propPointToPointSerial
Mon Jan 5 13:15:03 PST 2004 InOctets.1 0
Mon Jan 5 13:15:05 PST 2004 OutOctets.1 0

I want to be able to extract each line into a comma delimited list.
Bellow i am trying to print out my list print currentList but nothing
is comming out. I added a line after print test to make sure the file
has the information. Can someone help me with this?


#Read a file
true = 1
in_file = open("test.txt","r")
text = in_file.read()
while true:
in_line = in_file.readline()
if in_line == "":
break
currentList = string.split(in_line,",")
#print out the contents of the current list
print currentList
#make the inline stop reading
in_line = in_line[:-1]
in_file.close()


Here's an alternate way....

fd_in = open('myfiles/test_text.txt', 'r')
each_line = ''
a_list = []

for line in fd_in.readlines():
for word in line.split():
each_line += word + ','
a_list.append(word) # If a real list is desired
print each_line # Keep the trailing comma
print each_line.rstrip(',') # Delete the trailing comma
each_line = '' # Ready for the next line

#a_list = [] # Clear the list if one line per
# list is desired

print a_list # Otherwise the whole file is
# contained in the list

fd_in.close()


Prints:

This,is,line,one,
This,is,line,one
This,is,line,two,
This,is,line,two
This,is,line,three,
This,is,line,three
This,is,line,four,
This,is,line,four
This,is,line,five,
This,is,line,five
This,is,line,six,
This,is,line,six

['This', 'is', 'line', 'one', 'This', 'is', 'line', 'two', 'This', 'is',...]

Several concepts are embedded, if that's a confusion, I apologize.
Of course, you can write to an output file in lieu of the prints.

Norm
(just a newbie)
 

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,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top