Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
file not found error
-
@resserone13 yes, it is the reason why my code reads it in binary (rb).
-
@resserone13 in python you need to use forward slash
/
for file names. Backslash is interpreted as special characters. Hence \U is interpreted as a unicode character prefix. -
@JonB well seen, I did not remark this \ in the file name. I had a problem of ucode in my .csv, reason for which I used rb and I thought it was the same problem, shame on me.
-
@JonB thanks for the heads up on the backslashes. i havent worked with file that much and i never noticed.. thanks the isssue was a typo new it too but i had copied and pasted then i typed it. that along with the backslash error it was all mixed up... thanks for all you advice as always.
-
this was what i turned in for my homework. my teach said there was some name errors and that the file didnt run completely. maybe he was talking about the type of file i uploaded. it worked fine in my comp. i was using spyder only cus im on my comp. here was the homework and my code
Create a single Python script (.py) that completes the following steps:
Read the "US_state_populations.csv" file into Python using any method you like.
Create a new file called "alphabetic.csv". This file should contain the same 3 columns (state names, 2019 populations, and 2010 populations) separated by commas, but they should sorted alphabetically by the state name. You may write to this file using any method you like. Hint: When you sort the state names, make sure the 2010 and 2019 populations are sorted with their respective states.
Calculate the change in population between 2010 and 2019 (change = pop2019 - pop2010). Create a new file called "popchange.txt". This file should contain 2 columns: state names and population change, separated by a blank space. The file should contain a header line. You should write to this file manually using a for loop (not numpy, pandas, or any other library).
Upload your Python script, "alphabetic.csv" and "popchange.txt" to this Canvas assignment.list_of_list = [] #variable with file name. file_name = 'C:/Users/resse/Desktop/projects/poptest/US_state_populations2.csv' new_file = 'C:/Users/resse/Desktop/projects/poptest/abc2.csv' pop_diff = 'C:/Users/resse/Desktop/projects/poptest/pdiff.csv' #opens and closes file. with open(file_name, 'r') as f: #loops over file line by line. f.seek(23) for line in f: #saves each line as a list items = line.strip().split(',') #adds each list to a list list_of_list.append(items) #sorts the list s = sorted(list_of_list) #prints the sorted list print(s) #opens the new file and adds a sorted list to it. with open(new_file, 'w') as n_f: for i in s: n_f.write(f'\n {i[0]}, {i[1]}, {i[2]}') with open(pop_diff, 'w') as d_f: #writess the colum headers d_f.write(f'p19, p10, pdiff') #loops through a sorted list of states and population numbers for i in s: #calculates the population difference diff = int(i[1]) - int(i[2]) #writes each years population to a colum and #writes the population difference d_f.write(f'\n{i[1]}, {i[2]}, {diff}')
-
Obviously there are going to be file not found errors if you use an absolute path that doesnt exist on your professor's computer. You should use a relative path.
-
also, I ll point out that you didnt meet the file naming requirements-- you should be creating alphabetic.txt and popchange.txt...
-
@JonB your right. i submitted the wrong file types and the files were named incorrectly. ill have to pay more attention to things like that.. the were now working on a dice rolling exerecise and a mystery door game show ... ill make sure to submit the proper file types..
-
Why read .csv files without using https://docs.python.org/3/library/csv.html ??
-
@ccc i depends on how you wanted to go about it. the teacher mention that some students used csv module. its a really basic class. i used what was explained in class.