Yan
Yan

Reputation: 153

Read file to a list and then write the list to another file Python

list1 = []
with open('/home/yan/Desktop/a.txt','r') as f1:
    for line in f1:
        data = line.strip().split("\t")
        list1 += data[:2]
list2 = list(set(list1)


with open('/home/yan/Desktop/docs.txt','w') as f2:
    for item in list2:
      print>>f2,item

I'm trying to read the file a.txt to list1, delete some replicates and save it to list2, and then write list2 to docs.txt, but I get the Syntax error on the second with open, I don't know what's the problem here. Can anyone help me with it? Thanks!

The shell reports error on line7, and the message is "There's an error in your program:invalid syntax"

Upvotes: 0

Views: 221

Answers (1)

James Mills
James Mills

Reputation: 19050

Your SyntaxErro is coming from:

list2 = list(set(list1)

You forgot to close the parens around this expression.

This should be:

list2 = list(set(list1))

Upvotes: 2

Related Questions