On 28/03/18 11:07, theano orf wrote: > I am new in python and I am having problems of how to read a txt file and > insert the data in a list,
Just a quick response, but your data is more than a text file its a CSV file so the rules change slightly. Especially since you are using the csv module. Your data file is not a CSV file - it is just space separated and the string is not quoted so the CSV default mode of operation won;t work on this data as you seem to expect it to,. You will need to specify the separator (as what? A space wiill split on each word...) CSV might not be the best option here a simple string split combined with slicing might be better. > with open("training.txt", 'r') as file: The CSV module prefers binary files so open it with mode 'rb' not 'r' > reviews = list(csv.reader(file)) Try printing the first 2 lines of reviews to check what you have. I suspect it's not what you think. > positive_review = [r[1] for r in reviews if r[0] == str(1)] str(1) is just '1' so you might as well just use that. > after the print I only take an empty array. Why is this happening? I am > attaching also the training.txt file See the comments above about your data format. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor