<jupyter_start><jupyter_text>Udemy - Reading and writing text files Version 3.5.1 Created: 2016-05-08 Updated: 2016-05-08*****<jupyter_code>import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import sys
import os

# Set working directory
os.chdir("/Users/bradcannell/Dropbox/Python/UDemy")
os.getcwd()<jupyter_output><empty_output><jupyter_text>***  Reading in .csv file using read_csv<jupyter_code>dframe = pd.read_csv("data/lec25.csv")
dframe<jupyter_output><empty_output><jupyter_text>By default:    * Used comma as delimiter    * Used first row for column names<jupyter_code>dframe = pd.read_csv("data/lec25.csv", header = None)
dframe<jupyter_output><empty_output><jupyter_text>**Reading in .csv file using read_table (more generic)**<jupyter_code>dframe = pd.read_table("data/lec25.csv", sep = ",", header = None)
dframe<jupyter_output><empty_output><jupyter_text>**Reading in .csv file - subset of rows**<jupyter_code>pd.read_csv("data/lec25.csv", header = None, nrows = 2)<jupyter_output><empty_output><jupyter_text>***  Exporting .csv file<jupyter_code># Where dframe is the name of the data frame
dframe.to_csv("data/lec25_export.csv")

# Use sys to view output without an actual file being created
dframe.to_csv(sys.stdout)<jupyter_output>,0,1,2,3,4
0,q,r,s,t,apple
1,2,3,4,5,pear
2,a,s,d,f,rabbit
3,5,2,5,6,dog<jupyter_text>**Exporting using delimiters other than comma**<jupyter_code>dframe.to_csv(sys.stdout, sep = "_")

dframe.to_csv(sys.stdout, sep = "?")
dframe.to_csv(sys.stdout, sep = "\t")<jupyter_output>0	1	2	3	4
0	q	r	s	t	apple
1	2	3	4	5	pear
2	a	s	d	f	rabbit
3	5	2	5	6	dog<jupyter_text>**Export subset of columns**<jupyter_code>dframe.to_csv(sys.stdout, columns = [0, 1, 2])<jupyter_output>,0,1,2
0,q,r,s
1,2,3,4
2,a,s,d
3,5,2,5
{'fraction_non_alphanumeric': 0.14301801801801803, 'fraction_numerical': 0.04504504504504504, 'mean_word_length': 6.594017094017094, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '602730', 'license': 'MIT', 'n_tokens_mistral': 786, 'n_tokens_neox': 751, 'n_words': 157, 'path': 'working with data 1 - reading and writing text files.ipynb', 'repo_name': 'mbcann01/test-python', 'size': '1776'}