<jupyter_start><jupyter_text>2: Opening files InstructionsUse the open() function to create a File object. The name of the file is "crime_rates.csv" and we want the file to be accessed in read mode ("r"). Assign this File object to the variable f. Answer<jupyter_code>! touch test.txt
a = open("test.txt", "r")
print(a)
f = open("crime_rates.csv", "r")<jupyter_output><_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'><jupyter_text>3: Reading in files InstructionsRun the **read()** method on the File object **f** to return the *string* representation of **crime_rates.csv**. Assign the resulting *string* to the variable **data**.<jupyter_code>f = open("crime_rates.csv", "r")
data = f.read()
print(data)<jupyter_output>Albuquerque,749
Anaheim,371
Anchorage,828
Arlington,503
Atlanta,1379
Aurora,425
Austin,408
Bakersfield,542
Baltimore,1405
Boston,835
Buffalo,1288
Charlotte-Mecklenburg,647
Cincinnati,974
Cleveland,1383
Colorado Springs,455
Corpus Christi,658
Dallas,675
Denver,615
Detroit,2122
El Paso,423
Fort Wayne,362
Fort Worth,587
Fresno,543
Greensboro,563
Henderson,168
Houston,992
Indianapolis,1185
Jacksonville,617
Jersey City,734
Kansas City,1263
Las Vegas,784
Lexington,352
Lincoln,397
Long Beach,575
Los Angeles,481
Louisville Metro,598
Memphis,1750
Mesa,399
Miami,1172
Milwaukee,1294
Minneapolis,992
Mobile,522
Nashville,1216
New Orleans,815
New York,639
Newark,1154
Oakland,1993
Oklahoma City,919
Omaha,594
Philadelphia,1160
Phoenix,636
Pittsburgh,752
Plano,130
Portland,517
Raleigh,423
Riverside,443
Sacramento,738
San Antonio,503
San Diego,413
San Francisco,704
San Jose,363
Santa Ana,401
Seattle,597
St. Louis,1776
St. Paul,722
Stockton,1548
Tampa,616
Toledo,1171
Tucson,724
Tulsa,990
Virginia Beach,1[...]<jupyter_text>4: Splitting InstructionsSplit the string object data on the new-line character "\n" and store the result in a variable named rows. Then use the print() function to display the first 5 elements in rows. Answer<jupyter_code># We can split a string into a list.
sample = "john,plastic,joe"
split_list = sample.split(",")
print(split_list)

# Here's another example.
string_two = "How much wood\ncan a woodchuck chuck\nif a woodchuck\ncan chuck wood?"
split_string_two = string_two.split('\n')
print(split_string_two)

# Code from previous cells
f = open('crime_rates.csv', 'r')
data = f.read()
rows = data.split('\n')
print(rows[0:5])<jupyter_output>['john', 'plastic', 'joe']
['How much wood', 'can a woodchuck chuck', 'if a woodchuck', 'can chuck wood?']
['Albuquerque,749', 'Anaheim,371', 'Anchorage,828', 'Arlington,503', 'Atlanta,1379']<jupyter_text>5: Loops Instructions... Answer  6: Practice, loops InstructionsThe variable ten_rows contains the first 10 elements in rows. Write a for loop that iterates over each element in ten_rows and uses the print() function to display each element. Answer<jupyter_code>ten_rows = rows[0:10]
for row in ten_rows:
    print(row)<jupyter_output>Albuquerque,749
Anaheim,371
Anchorage,828
Arlington,503
Atlanta,1379
Aurora,425
Austin,408
Bakersfield,542
Baltimore,1405
Boston,835<jupyter_text>7: List of lists InstructionsFor now, explore and run the code we dissected in this step in the code cell below Answer<jupyter_code>three_rows = ["Albuquerque,749", "Anaheim,371", "Anchorage,828"]
final_list = []
for row in three_rows:
    split_list = row.split(',')
    final_list.append(split_list)
print(final_list)
for elem in final_list:
    print(elem)
print(final_list[0])
print(final_list[1])
print(final_list[2])<jupyter_output>[['Albuquerque', '749'], ['Anaheim', '371'], ['Anchorage', '828']]
['Albuquerque', '749']
['Anaheim', '371']
['Anchorage', '828']
['Albuquerque', '749']
['Anaheim', '371']
['Anchorage', '828']<jupyter_text>8: Practice, splitting elements in a listLet's now convert the full dataset, rows, into a list of lists using the same logic from the step before. InstructionsWrite a for loop that splits each element in rows on the comma delimiter and appends the resulting list to a new list named final_data. Then, display the first 5 elements in final_data using list slicing and the print() function. Answer<jupyter_code>f = open('crime_rates.csv', 'r')
data = f.read()
rows = data.split('\n')
final_data = [row.split(",")
              for row in rows]
print(final_data[0:5])<jupyter_output>[['Albuquerque', '749'], ['Anaheim', '371'], ['Anchorage', '828'], ['Arlington', '503'], ['Atlanta', '1379']]<jupyter_text>9: Accessing elements in a list of lists, the manual way Instructionsfive_elements contains the first 5 elements from final_data. Create a list of strings named cities_list that contains the city names from each list in five_elements. Answer<jupyter_code>five_elements = final_data[:5]
print(five_elements)
cities_list = [city for city,_ in five_elements]<jupyter_output>[['Albuquerque', '749'], ['Anaheim', '371'], ['Anchorage', '828'], ['Arlington', '503'], ['Atlanta', '1379']]<jupyter_text>10: Looping through a list of lists InstructionsCreate a list of strings named cities_list that contains just the city names from final_data. Recall that the city name is located at index 0 for each list in final_data. Answer<jupyter_code>crime_rates = []

for row in five_elements:
    # row is a list variable, not a string.
    crime_rate = row[1]
    # crime_rate is a string, the city name.
    crime_rates.append(crime_rate)
    
cities_list = [row[0] for row in final_data]<jupyter_output><empty_output><jupyter_text>11: Practice InstructionsCreate a list of integers named int_crime_rates that contains just the crime rates as integers from the list rows.First create an empty list and assign it to int_crime_rates. Then, write a for loop that iterates over rows that executes the following:* uses the split() method to convert each string in rows into a list on the comma delimiter* converts the value at index 1 from that list to an integer using the int() function* then uses the append() method to add each integer to int_crime_rates<jupyter_code>f = open('crime_rates.csv', 'r')
data = f.read()
rows = data.split('\n')
print(rows[0:5])
int_crime_rates = []

for row in rows:
    data = row.split(",")
    if len(data) < 2:
        continue
    int_crime_rates.append(int(row.split(",")[1]))

print(int_crime_rates)<jupyter_output>[749, 371, 828, 503, 1379, 425, 408, 542, 1405, 835, 1288, 647, 974, 1383, 455, 658, 675, 615, 2122, 423, 362, 587, 543, 563, 168, 992, 1185, 617, 734, 1263, 784, 352, 397, 575, 481, 598, 1750, 399, 1172, 1294, 992, 522, 1216, 815, 639, 1154, 1993, 919, 594, 1160, 636, 752, 130, 517, 423, 443, 738, 503, 413, 704, 363, 401, 597, 1776, 722, 1548, 616, 1171, 724, 990, 169, 1177, 742]
{'fraction_non_alphanumeric': 0.11889373215090937, 'fraction_numerical': 0.09289042537201263, 'mean_word_length': 6.018987341772152, 'pattern_counts': {'":': 0, '<': 31, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '494499', 'license': 'MIT', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2313, 'n_words': 766, 'path': 'python_introduction/beginner/.ipynb_checkpoints/files and loops-checkpoint.ipynb', 'repo_name': 'datascience-practice/data-quest', 'size': '6653'}