0

I have a list:

mylist=[(u'computer', 1592), (u'student', 1113), (u'university', 1080), (u'raspberry', 1000), (u'science', 814), (u'$5', 770), (u'pi', 688), (u'exam', 571), (u'just', 544), (u'intelligence', 495), (u'solution', 475), (u'costs', 423), (u'exam:', 411), (u'latest', 402), (u"pi's", 366), (u'be', 311), (u'can', 268), (u'what', 268), (u'way', 257), (u'students', 238)]

How can I write that list on a txt file line by line?, this way:

('computer', 1592)

('student', 1113)

('university', 1080)

... ... ...

and how can i write a dictionary like this

d = {u'computer':1592 , u'student':1113,........}
Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286
Nicqu
  • 39
  • 1
  • 5
  • Have you read about File I/O ... https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files – Iron Fist Dec 06 '15 at 17:52
  • 1
    Possible duplicate of [Python: Write a list to a file](http://stackoverflow.com/questions/899103/python-write-a-list-to-a-file) – Iron Fist Dec 06 '15 at 17:57
  • You need to be more specific in the format. The tuples have quotes but not the `u`? Are these things supposed to look like python data structures when written? – tdelaney Dec 06 '15 at 18:16
  • @Nicqu, writing as tuples makes no sense unless you maybe pickle the data, use the csv module as per the first part of my answer or if you are going to be using a dict then just create a dict from the data and dump that with json. – Padraic Cunningham Dec 06 '15 at 18:29

3 Answers3

2

mylist=[(u'computer', 1592), (u'student', 1113), (u'university', 1080), (u'raspberry', 1000), (u'science', 814), (u'$5', 770), (u'pi', 688), (u'exam', 571), (u'just', 544), (u'intelligence', 495), (u'solution', 475), (u'costs', 423), (u'exam:', 411), (u'latest', 402), (u"pi's", 366), (u'be', 311), (u'can', 268), (u'what', 268), (u'way', 257), (u'students', 238)]

text_file = open("Output.txt", "w")

for element in mylist:

    text_file.write("('" + str(element[0]) + "', " + str(element[1]) + ")")

    text_file.write("\n")

text_file.close()

here is the output:

('computer', 1592)

('student', 1113)

('university', 1080)

('raspberry', 1000)

('science', 814)

('$5', 770)

('pi', 688)

('exam', 571)

('just', 544)

('intelligence', 495)

('solution', 475)

('costs', 423)

('exam:', 411)

('latest', 402)

('pi's', 366)

('be', 311)

('can', 268)

('what', 268)

('way', 257)

('students', 238)

Sunit
  • 382
  • 1
  • 12
1

If you really want it as one pair per line and to create a dict separately use the csv module and forget the tuple:

import csv
with open("tup.txt","w") as f:
    csv.writer(f).writerows(mylist)

Output:

computer,1592
student,1113
university,1080
raspberry,1000
science,814
$5,770
pi,688
exam,571
just,544
intelligence,495
solution,475
costs,423
exam:,411
latest,402
pi's,366
be,311
can,268
what,268
way,257
students,238

and to create the dict just call dict on myList:

 d = dict(myList)

Output:

{u'be': 311, u'what': 268, u'$5': 770, u'exam': 571, u'just': 544, u'students': 238, u'science': 814, u'university': 1080, u'way': 257, u'solution': 475, u'costs': 423, u"pi's": 366, u'computer': 1592, u'can': 268, u'student': 1113, u'intelligence': 495, u'pi': 688, u'raspberry': 1000, u'exam:': 411, u'latest': 402}

If you essentially want a dict then you can just create and dump a dict using json:

import json


with open("data.json","w") as f:
    json.dump(dict(mylist), f)

Which will store your data as:

{"what": 268, "science": 814, "pi's": 366, "can": 268, "be": 311, "exam:": 411, "university": 1080, "costs": 423, "intelligence": 495, "latest": 402, "just": 544, "solution": 475, "$5": 770, "raspberry": 1000, "student": 1113, "way": 257, "computer": 1592, "exam": 571, "students": 238, "pi": 688}

Then to load again:

with open("data.json") as f:
    d = json.load(f)
    print(d)

Which will give you a dict again:

{'raspberry': 1000, 'exam': 571, 'what': 268, 'be': 311, 'intelligence': 495, 'latest': 402, 'computer': 1592, 'university': 1080, '$5': 770, 'science': 814, 'can': 268, 'costs': 423, 'students': 238, 'solution': 475, 'student': 1113, 'pi': 688, 'exam:': 411, 'just': 544, 'way': 257, "pi's": 366}
Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286
  • @Nicqu, if you want a dict and plan on accessing as a dict it makes sense to store as a dict, writing the tuple representation as a string is going to be absolutely of no use to you – Padraic Cunningham Dec 06 '15 at 18:53
0

Since you are working with unicode data you need to decide on an encoding for the file because non-ascii characters can't be written to the file natively. "utf-8" is the most widely accepted format, so I'm going with that. Your dict looks like you just want the python representation of a dict but your list is a bit different (the "u" at the front is missing). So, I did them a bit differently.

This script

mylist=[(u'computer', 1592), (u'student', 1113), (u'university', 1080), (u'raspberry', 1000), (u'science', 814), (u'$5', 770), (u'pi', 688), (u'exam', 571), (u'just', 544), (u'intelligence', 495), (u'solution', 475), (u'costs', 423), (u'exam:', 411), (u'latest', 402), (u"pi's", 366), (u'be', 311), (u'can', 268), (u'what', 268), (u'way', 257), (u'students', 238)]

with open('test1.txt', 'w') as fp:
    # write line by line without python 2 style string encoding marker
    for item in mylist:
        fp.write(u"('{}', {})\n".format(*item).encode('utf-8'))
    fp.write('\n')

    # write python 2 representation of a dict
    mydict = dict(mylist)
    fp.write(repr(mydict).encode('utf-8'))
    fp.write('\n')

print open('test1.txt').read().decode('utf-8')

produces these results

('computer', 1592)
('student', 1113)
('university', 1080)
('raspberry', 1000)
('science', 814)
('$5', 770)
('pi', 688)
('exam', 571)
('just', 544)
('intelligence', 495)
('solution', 475)
('costs', 423)
('exam:', 411)
('latest', 402)
('pi's', 366)
('be', 311)
('can', 268)
('what', 268)
('way', 257)
('students', 238)

{u'be': 311, u'what': 268, u'$5': 770, u'exam': 571, u'just': 544, u'students': 238, u'science': 814, u'university': 1080, u'way': 257, u'solution': 475, u'costs': 423, u"pi's": 366, u'computer': 1592, u'can': 268, u'student': 1113, u'intelligence': 495, u'pi': 688, u'raspberry': 1000, u'exam:': 411, u'latest': 402}
tdelaney
  • 55,698
  • 4
  • 59
  • 89