Hi I am a Pyth noob and wanted to import a text file .To each line of the text file , assign a (fixed) name and sum a number (in increments of 1 from the first line to the last)using the while command for an assigned name.

Lets say I choose foo as an 'identifier' :

foo = 'line number'
while foo <=10:
print foo
foo+=1


I have 3 problems :

1) I do not know how to open/load the txt file.
2) I want to sum the word: linenumber and a numer in increments of 1 and make the content of the line a string so I can manipulate it ( with the name: linenumber+number in increment of one)
3) Do not know how many lines I will be loading and want it to make it automatically with a loop and break funtion.

Many thanks

To read a file use:

fileHandle = open ( 'text.txt', 'r' )
str1 = fileHandle.read()
fileHandle.close()
print str1
break

To write data to a file use:

fileHandle = open ( 'text.txt', 'w' )
fileHandle.write(str1)
fileHandle.close()

To append data to an existing file use:

fileHandle = open ( 'text.txt', 'a' )
fileHandle.write(str1)
fileHandle.close()

With the code for reading a text file you can also leave the 'r' out as by default Python will read a file. The way these pieces of code are done is that it will write the data as a string. Well it does for me when I use them.

Edited 6 Years Ago by Simes: n/a

You can keep track of the lines of text by using a list and the index number ...

# text for the test
test_text = """\
apple
banana
lemon
melon
pear"""

fname = "myfruits.txt"
#write the multiline text to a test file
fout = open(fname, "w")
fout.write(test_text)
fout.close()

# read the file back as a list of lines
fin = open(fname, "r")
data_list = fin.readlines()
fin.close()

print data_list

"""my output -->
['apple\n', 'banana\n', 'lemon\n', 'melon\n', 'pear']
"""

# optionally strip the trailing newline char '\n'
data_list = [item.rstrip('\n') for item in data_list]

print data_list

"""my output -->
['apple', 'banana', 'lemon', 'melon', 'pear']
"""

# now you can access each line (list item) by the index
# the index of a list is zero based
print data_list[0]  # apple
print data_list[3]  # melon
Comments
You blew my hat off, excelent, really thrilled me

You can keep track of the lines of text by using a list and the index number ...

# text for the test
test_text = """\
apple
banana
lemon
melon
pear"""

fname = "myfruits.txt"
#write the multiline text to a test file
fout = open(fname, "w")
fout.write(test_text)
fout.close()

# read the file back as a list of lines
fin = open(fname, "r")
data_list = fin.readlines()
fin.close()

print data_list

"""my output -->
['apple\n', 'banana\n', 'lemon\n', 'melon\n', 'pear']
"""

# optionally strip the trailing newline char '\n'
data_list = [item.rstrip('\n') for item in data_list]

print data_list

"""my output -->
['apple', 'banana', 'lemon', 'melon', 'pear']
"""

# now you can access each line (list item) by the index
# the index of a list is zero based
print data_list[0]  # apple
print data_list[3]  # melon

many thanks guys very valuable input. I am still in the process of learning and I can't still figure out many things as I have not studied them still. thanks again.


@vegaseat: very clear code. as mentioned before I am a complete noob but your code is like reading a good child's book ( easy and concise)

Edited 6 Years Ago by elvis1: n/a

This article has been dead for over six months. Start a new discussion instead.