mercredi 4 novembre 2015

Read from a file character by character in python, including special symbols

I have a text file from which I wish to read. testfile.txt (contains hi i love chinese food., :) ) is in the same folder as my python code. I tried

file = open('testfile.txt', 'r')

for line in file.readlines():

for c in line:

    if c == 'a' or 'A':
         do this
    if c == ',':
         do this


BUT when I run the code It gets stuck at h and doesn't move ahead. What is wrong with the code? (I have conditions for all the words) Q2. I am using python 2.7 and I am not including any loop that reads till EOF. Will it automatically read all the lines till EOF? and will it get terminated at \n? (P.S.: I looked thoroughly on stack overflow but couldn't find the answer)

I tried to run the code separately also, it prints all words in different line.

Aucun commentaire:

Enregistrer un commentaire