Input Output
import json as json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random as random
%matplotlib inline
Txt file
A subdirectory or file data already exists.
with open('data/test.txt', 'w')as file:
for k in range(100):
file.write("it is your "+ str(k) + "th turn! \n")
open()
and close()
method
txtfile = open("data/textdata.txt",'w')
for i in range (1000):
'''check even or odd'''
if i%2 ==0:
txtfile.write(str(i) + "|It is even \n")
'''check above or below 500'''
if i<500:
txtfile.write(str(i) + "|It is below 500 \n")
else:
txtfile.write(str(i) + "|It is above 500 \n")
else:
txtfile.write(str(i) + "|It is odd \n")
'''check above or below 500'''
if i<500:
txtfile.write(str(i) + "|It is below 500 \n")
else:
txtfile.write(str(i) + "|It is above 500 \n")
txtfile.close()
- uploading/reading text file
with open('data/textdata.txt','r') as f:
i = 0
for line in f:
print(line)
if i >10:
break
i = i+1
0|It is even
0|It is below 500
1|It is odd
1|It is below 500
2|It is even
2|It is below 500
3|It is odd
3|It is below 500
4|It is even
4|It is below 500
5|It is odd
5|It is below 500
JSON file
Data = []
for k in range(1000):
Data.append({"x": np.random.uniform(0,10),\
"y": np.random.uniform(0,20),\
"z" : np.random.uniform(0,10)})
[{'x': 6.1054636810164595, 'y': 3.2863675264349634, 'z': 8.384891787980322},
{'x': 7.069985836956237, 'y': 0.17988344698211423, 'z': 9.975981385348762},
{'x': 2.7021265268361416, 'y': 9.729258889243164, 'z': 9.894201832125768},
{'x': 6.418458045982539, 'y': 12.858408084598345, 'z': 5.91904399119246}]
A subdirectory or file data already exists.
with open("data/sample_data.json", 'w')as f:
json.dump(Data,f)
- Upload data from JSON file
with open("data/sample_data.json","r")as f:
uploaded_data = json.load(f)
[{'x': 0.6982478944780635, 'y': 1.126719593276797, 'z': 0.4844149246453111},
{'x': 9.508377504777027, 'y': 15.488536320226022, 'z': 2.7307796829523037}]
CSV file
DF = pd.DataFrame(Data)
DF.head()
|
x |
y |
z |
0 |
0.698248 |
1.126720 |
0.484415 |
1 |
9.508378 |
15.488536 |
2.730780 |
2 |
7.248392 |
1.646036 |
5.782741 |
3 |
5.829758 |
4.888937 |
7.824382 |
4 |
3.174409 |
19.844952 |
4.117781 |
DF.to_csv("data/sampledata.csv")
- Upload data from a CSV file
uploaded_data = pd.read_csv("data/sampledata.csv")
|
Unnamed: 0 |
x |
y |
z |
0 |
0 |
0.698248 |
1.126720 |
0.484415 |
1 |
1 |
9.508378 |
15.488536 |
2.730780 |
2 |
2 |
7.248392 |
1.646036 |
5.782741 |
3 |
3 |
5.829758 |
4.888937 |
7.824382 |
4 |
4 |
3.174409 |
19.844952 |
4.117781 |