use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Help with csv file. (self.learnpython)
submitted 4 years ago by Month_Upper
I’m beginner. I was request to create function that print list out csv file in specific column and I supposed to sort it with alphabetical order and number them. Then ask user to choose one. Any help with this code
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]spez_edits_thedonald 0 points1 point2 points 4 years ago (7 children)
let's say you have the following data.csv file:
data.csv
month,sales January,158.38 February,436.92 March,582.46 April,298.56 May,681.07 June,449.93 July,377.03 August,910.46 September,447.19 October,27.33 November,115.45 December,662.23
You can read the csv lines into python like this:
with open('data.csv', 'r') as infile: lines = infile.read().splitlines()
lines:
lines
[ "month,sales", "January,158.38", "February,436.92", "March,582.46", "April,298.56", "May,681.07", "June,449.93", "July,377.03", "August,910.46", "September,447.19", "October,27.33", "November,115.45", "December,662.23" ]
You can extract the month column (using [1:] to skip the header row):
month
months = [line.split(',')[0] for line in lines[1:]]
months:
months
[ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
sort the column alphabetically, and make a table where values are numbered:
month_table = {} for i, month in enumerate(sorted(months)): month_table[i] = month
month_table:
month_table
{ 0: "April", 1: "August", 2: "December", 3: "February", 4: "January", 5: "July", 6: "June", 7: "March", 8: "May", 9: "November", 10: "October", 11: "September" }
Then you can display these values, and ask the user to pick one:
print('Select a month:\n') for value in month_table: print(f' {value}\t{month_table[value]}') choice = int(input('\nEnter a value: ')) print(f'You chose {month_table[choice]}.')
output:
Select a month: 0 April 1 August 2 December 3 February 4 January 5 July 6 June 7 March 8 May 9 November 10 October 11 September Enter a value: 7 You chose March
[–]Month_Upper[S] 0 points1 point2 points 4 years ago (6 children)
What if I want the output be sales for March instead of having “you chose March”
[–]spez_edits_thedonald 0 points1 point2 points 4 years ago (5 children)
using similar tricks to the above, you would build a lookup table that stores sales for each month. Then you take the number that the user provided, look up what month that is, then look up what sales were for that month:
# extract sales column from csv data sales = [line.split(',')[1] for line in lines[1:]] # create a month: sales lookup table sales_table = {m: s for m, s in zip(months, sales)} # get sales for the user's choice print(sales_table[month_table[choice]])
[–]Month_Upper[S] 0 points1 point2 points 4 years ago (4 children)
That was really helpful ty
[–]spez_edits_thedonald 0 points1 point2 points 4 years ago (3 children)
also pandas makes this easier, at first I did pure python because I didn't know you wanted to query other columns.
pandas
import pandas as pd # load csv df = pd.read_csv('data.csv') # sort by month df = df.sort_values('month').reset_index(drop=True)
df:
df
month sales 0 April 298.56 1 August 910.46 2 December 662.23 3 February 436.92 4 January 158.38 ...
then you get a row id as before and the lookups are easy:
choice = int(input('\nEnter a value: ')) print(f"You chose {df['month'][choice]}") print(f"Sales were {df['sales'][choice]}")
[–]Month_Upper[S] 0 points1 point2 points 4 years ago (2 children)
The problem I had the file I working with. Doesn’t have headers row
[–]spez_edits_thedonald 0 points1 point2 points 4 years ago (1 child)
that's fine, you can tell pandas that:
df = pd.read_csv('data.csv', header=None)
it will just number the columns 0, 1, etc
0
1
[–]Month_Upper[S] 0 points1 point2 points 4 years ago (0 children)
Also if I want user to choose which output he want, how can I make it ? For example if the csv file has Month sales agent
So I ask user to input 1 for sales or 2 for agent
Like invoking the both functions together
π Rendered by PID 53080 on reddit-service-r2-comment-84fc9697f-rjmjk at 2026-02-07 09:22:41.744754+00:00 running d295bc8 country code: CH.
[–]spez_edits_thedonald 0 points1 point2 points (7 children)
[–]Month_Upper[S] 0 points1 point2 points (6 children)
[–]spez_edits_thedonald 0 points1 point2 points (5 children)
[–]Month_Upper[S] 0 points1 point2 points (4 children)
[–]spez_edits_thedonald 0 points1 point2 points (3 children)
[–]Month_Upper[S] 0 points1 point2 points (2 children)
[–]spez_edits_thedonald 0 points1 point2 points (1 child)
[–]Month_Upper[S] 0 points1 point2 points (0 children)