i got error while writing data to file

Ben Iannitelli cannedham284 at hotmail.com
Mon Mar 13 12:42:27 EDT 2017


Hi Madhu,

I don't know much about web scraping but I can explain why your output is so much smaller than your input: you keep writing over your output file. The way I see it you have two options:

1. Where you wrote "mode='wt'", change "wt" to "at". The replacement "at" appends your output text, as opposed to "wt", which writes over your output text as if the file hadn't already existed.

2. Leave the file modes alone,  but move the "with" statement further up the "for" block. If you use this option, your code should look more like below (if email didn't garble the formatting):

with open('E:/amadown2py-master/reviews1.csv', 'r',encoding='UTF8') as csvfile:
    mycsv = csv.reader(csvfile)
    for row in mycsv:
        data = row[0]
        #print (data)
        with open('E:/amadown2py-master/Sam7_pos_rev.txt',mode='wt') as myfile:#Moved this!
            try:
                score = ast.literal_eval(row[1])
                if score > 3:
                    cnt=0;
                    #print (score)
                    print (data)
                    positivedata.append((data))
                    myfile.writelines('\n'.join(positivedata))
                    #myfile.close()#You shouldn't need this, bc you're using "with"

Of the two options, I would think that option 1 is easier.

HTH,

Ben I.



More information about the Python-list mailing list