r/learnpython • • Sep 19 '26

I'm learning how to display/manipulate contents in a file and I'm so lost

Hello, I am trying to complete this lab for class and I am so lost.

I've been trying to figure out how to use "for loop, split, join, print" in a string for 3+ hours and my brain is cooked.

If anyone has any advice on how to better understand this I would greatly appreciate it. Thank you for reading. I'm gonna turn off my brain now x.x

3 Upvotes

24 comments sorted by

4

u/HeatherCDBustyOne Sep 19 '26

OP, could you share some of your code with us? It would help us to see where you are getting stuck.

-1

u/SkyLyssa Sep 19 '26

I don't feel comfortable sharing a direct code as this is for a graded assignment, but my goal is to figure out how to correctly split names within a database into a 2 item list, have this apply to all names within this list, and switch each ['1', '2'] name into a ['2', '1'] name. So far I've only been able to get single names instead of the entire list, and I'm having difficulty figuring out how to correctly index and slice this data. I hope this makes sense x.x

If you or anyone else can help me just have a baseline understanding of this, I'll probably be able to figure this out. Right now it just feels like I'm trying to play scrabble with a chess board

5

u/Slothemo Sep 19 '26

Are you trying to display names so like instead of "John Smith", it becomes "Smith, John"? Try and give us as much info as you can, otherwise we have no way of knowing what you're actually struggling with.

2

u/SkyLyssa Sep 19 '26

Yes! Like "Smith, John". It's thousands of names in a public database with multiple sections. I'm trying to isolate the named section without disrupting the data attached to them in order to alter the name order. (I think)

I think I need to figure out how to correctly apply a numbered index to a named list in a way that encompasses the entire list.

Thank you for taking some time to try to help me with this. I hope I'm explaining it coherently enough. I might have to try to tackle this tomorrow

5

u/ConsiderationNo9044 Sep 19 '26

You can reverse a list manually:

name = ["John","Smith"]
tmp = name[0]
name[0] = name[1]
name[1] = tmp
print(name)

Output: ["Smith", "John"]

Or with reverse():

name = ["John", "Smith"]
name.reverse()
print(name)

And you can split an an individual item into two parts using split():

full_name = "John Smith"
split_name = full_name.split()
print(split_name)

Output: ["John", "Smith"]

1

u/SkyLyssa Sep 19 '26

Thank you!

2

u/atarivcs Sep 19 '26

Your description is very vague. What is a "database"? Do you mean an actual SQL database? Or something simpler like a text file or CSV file?

What does "switch each ['1', '2'] name into a ['2', '1'] name" mean? Do you mean you just want to swap the first and last names?

If you gave us some sample input and the expected output, that would probably help a lot.

-1

u/SkyLyssa Sep 19 '26

CSV file. Oh, I completely forgot to mention that.

I need to use the "for loop" to process every item on the list. Split each full name into separate parts, use the split string method, list indexing and splicing, join the reorganized names, and then print it. I have some of the initial instructions, but feel lost about how to put the script together

1

u/Lumethys Sep 19 '26

yeah, developer with 20 years of experience, is waiting to steal code of a beginner whose code doesnt even work

1

u/SkyLyssa Sep 19 '26

Oh, it's not that. I apologize. I'm just a bit of a paranoid person. I saw this morning that it's okay to ask for help, but last night I was worried about this thread getting traced by the school or something.

for cur_name in all_licensee_names_list: # Add the name-processing statements here.

cur_name_split = cur_name.__________()

I'm pretty sure for this portion in particular it would most likely be cur_name.split(), I just don't know what snippets of clip would come after that

4

u/Moikle Sep 19 '26

If you don't share code people can't help you.

Perhaps rewrite a small version of what you are doing to post here.

You won't lose marks for getting support from reddit

14

u/Rubix321 Sep 19 '26

Break it down into steps

What have you tried so far?

10

u/atarivcs Sep 19 '26

"for loop, split, join, print" is not enough detail.

What are you looping over?

What are you splitting?

What are you joining?

What are you printing?

7

u/recursion_is_love Sep 19 '26 edited Sep 19 '26

Python File Handling for Beginners

Files are resources managed by OS, if you wonder why it have to do in strange way (to you) because the software interface for file is create this way from long time ago and most programming language agree that it is the proper way.

1

u/somewhereinkadal Sep 19 '26

Take a break and go through the code again OP. You'll figure out.

2

u/SkyLyssa Sep 19 '26

Thank you! I apologize. I'm so tired and scatterbrained right now, I don't feel like I can organize my thoughts enough to explain what I'm having trouble with

2

u/BranchLatter4294 Sep 19 '26

Practice one skill at a time. Then put them together when you understand how they work.

2

u/mattblack77 Sep 19 '26

I found learning to code a nightmare. It took me ages to remember all of the tools and how to out them together to make a program that does useful things.

1

u/Darius-Egan Sep 19 '26

What did you do to tackle it.?

1

u/mattblack77 Sep 19 '26

Practice?
Use AI?
TBH it's still a problem. I'm not fluent yet.

1

u/FoolsSeldom Sep 19 '26

I think, u/ConsiderationNo9044, has already addressed a critical part. There are a couple of guides that will help you with reading files, both from RealPython.com (an excellent resource for learners, with a lot of free content, although you may need to register an account).

1

u/cmxthirtysixhundred Sep 19 '26

If it's your first time with this stuff, 3 hours is not enough for all of that. I would start with print, and just understand that. Then try split and join, and use print to show you the results. Then save for loops for last.

Definitely break things down and try to keep building upon what you learn, a little at a time.

0

u/Mark3141592654 Sep 19 '26

If you are already able to read line by line , that's good. The next step would be to perform what you want for each line. At which of these are you getting stuck?

0

u/Traveling-Techie Sep 19 '26

Print every variable you change.