I want to iterate through a list in python and printing it with the word 'hello' after it. However, I am not getting the desired output.
The code reads a text file which has numeric ID's. I have created a list of the read ID and used a for loop to iterate through it. Following is my code:
def read_file():
pmid_s = []
file_object = open("file.txt", "r", encoding='cp1252')
pmid =file_object.read()
pmid_s.append(pmid)
for pmid in pmid_s:
return(pmid)
def driver(pmid):
print("hello"+pmid)
def main():
pmid= read_file
driver(pmid)
if __name__ == '__main__':
main()
The desired output is as follows in new line:
hello1
hello2
hello3
hello4
hello5
driverinstead ofprint? And why are you redefining theprintfunction?!read(). You could trypmid_s = list(file_object). This is going to give you the lines.read_filesupposed to do? You read the entire contents into one variable, append that to an empty list, then iterate over the singleton list. The entire function could be simplified toread_file = lambda: open(...).read().