1# define list of places
2places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
3
4with open('listfile.txt', 'w') as filehandle:
5 for listitem in places:
6 filehandle.write('%s\n' % listitem)
1color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
2with open('file1.txt','w+') as f:
3 for i in color:
4 f.write('%s\n'%i)
5
6with open('file1.txt') as f:
7 print(f.read())
8###############
9list1=[]
10with open('file1.txt','r+') as f:
11 lines = f.readlines()
12 for line in lines:
13 item = line[:-1]
14 list1.append(item)
15 print(list1)
16#############
17
18with open('file1.txt','r') as f:
19 lines=f.read()
20 listli=lines.split('\n')
21 print(listli)
22 # listli=lines.strip()
23 # print(listli)
24
1import json
2a = [1,2,3]
3with open('test.txt', 'w') as f:
4 f.write(json.dumps(a))
5
6#Now read the file back into a Python list object
7with open('test.txt', 'r') as f:
8 a = json.loads(f.read())
1with open('your_file.txt', 'w') as f:
2 for item in my_list:
3 f.write("%s\n" % item)
4
1# attempt #1
2f = open("Bills.txt", "w")
3f.write("\n".join(map(lambda x: str(x), bill_List)))
4f.close()
5
6
7# attempt #2
8# Open a file in write mode
9f = open('Bills.txt', 'w')
10for item in bill_List:
11f.write("%s\n" % item)
12# Close opend file
13f.close()
14
15# attempt #3
16
17with open('Bills.txt', 'w') as f:
18for s in bill_List:
19 f.write(s + '\n')
20
21with open('Bills.txt', 'r') as f:
22bill_List = [line.rstrip('\n') for line in f]
23
24# attempt #4
25with open('Bills.txt', 'w') as out_file:
26out_file.write('\n'.join(
27 bill_List))
1# define list of places
2places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
3
4with open('listfile.txt', 'w') as filehandle:
5 for listitem in places:
6 filehandle.write('%s\n' % listitem)
7