0% found this document useful (0 votes)
4 views

text file and JSON .ipynb - Colab

The document contains Python code for file operations, including writing to, reading from, and appending to a text file. It also demonstrates how to work with JSON data, including parsing a JSON string and converting a Python dictionary to a JSON string. The code snippets show the output of each operation performed on the text file and JSON data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

text file and JSON .ipynb - Colab

The document contains Python code for file operations, including writing to, reading from, and appending to a text file. It also demonstrates how to work with JSON data, including parsing a JSON string and converting a Python dictionary to a JSON string. The code snippets show the output of each operation performed on the text file and JSON data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

2/14/25, 11:11 AM text file and JSON 149.

ipynb - Colab

f=open("D:\textfile.txt",'w')
f.write("first line\n")
f.write('Second line\n')
f.write("third line\n")
f.close()

k=open("/content/D: extfile.txt",'r')
print(k.readline())
print(k.readline())
print(k.readline())
k.close()

first line

Second line

third line

m=open("/content/D: extfile.txt",'a')
m.write("fourth line\n")
m.write("fifth line\n")
m.close()

p=open("/content/D: extfile.txt",'r')
print(p.read())
p.close()

first line
Second line
third line
fourth line
fifth line

with open("/content/D: extfile.txt",'r')as file:


lines=file.readlines()
print(lines)

['first line\n', 'Second line\n', 'third line\n', 'fourth line\n', 'fifth line\n']

import json
x ='{"name":"John","age":30,"city":"New York"}'
y=json.loads(x)
print(y["age"])

30

x
{
"name":"John",
"age":30,
"city":"New York"
}

{'name': 'John', 'age': 30, 'city': 'New York'}

y=json.dumps(x)
print(y)

"{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"

https://colab.research.google.com/drive/1NXassKHR8HSVRiGh_pQJMANJQE7dWgG_#scrollTo=SaektmUR8XYd&printMode=true 1/2

You might also like