<jupyter_start><jupyter_text>Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved MIT license. (c) <NAME> and Others<jupyter_code>from __future__ import print_function

%matplotlib inline
import numpy
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Reading and Writing Data1. Basic reading1. `loadtxt`1. Basic writing1. Reading Tabular Data  Basic Reading of Data<jupyter_code>import os

path = os.path.join(os.getcwd(), "data", "sunspots.txt")

years = []
spot_count = []
with open(path, 'r') as sunspot_data:
    for line in sunspot_data:
        years.append(int(line.split()[0]))
        spot_count.append(int(line.split()[1]))

plt.plot(years, spot_count, 'k-')
plt.show()<jupyter_output><empty_output><jupyter_text>Using `loadtxt`<jupyter_code>import os

path = os.path.join(os.getcwd(), "data", "sunspots.txt")

data = numpy.loadtxt(path, dtype=int)
years = data[:, 0]
spot_count = data[:, 1]

plt.plot(years, spot_count, 'k-')
plt.show()<jupyter_output><empty_output><jupyter_text>Reading Tabular Data  Basic Writing of Data<jupyter_code>import os
import numpy.random

path = os.path.join(os.getcwd(), "data", "mydata.txt")

sample_number = numpy.arange(1, 101)
my_data = numpy.random.normal(size=(100))

# with open(path, 'w') as mydata_file:
#     for (i, N) in enumerate(sample_number):
#         mydata_file.write("{}, {}\n".format(N, my_data[i]))

numpy.savetxt?
# numpy.savetxt(path, my_data)
    
# Test data
data = numpy.loadtxt(path, delimiter=',')
read_data = data[:, 1]

fig = plt.figure()
fig.set_figwidth(fig.get_figwidth() * 2)

axes = fig.add_subplot(1, 2, 1)
axes.hist(my_data)

axes = fig.add_subplot(1, 2, 2)
axes.hist(read_data)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15044742729306487, 'fraction_numerical': 0.012863534675615212, 'mean_word_length': 5.529197080291971, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '283546', 'license': 'MIT', 'n_tokens_mistral': 709, 'n_tokens_neox': 678, 'n_words': 147, 'path': 'intro_python/05_data_io.ipynb', 'repo_name': 'mandli/short-courses', 'size': '1796'}