Quck question
engsolnom at ipns.com
engsolnom at ipns.com
Mon Jan 5 20:41:02 EST 2004
On 5 Jan 2004 16:10:21 -0800, sinzcere at hotmail.com (J) wrote:
>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)
More information about the Python-list
mailing list