Writing appended list to File... Hacking the Fender Task

Hello,

Currently I am completing a project: Hacking the Fender for Python 3 module.

My issue is that I am not understanding why, when writing the compromised_users.txt file, the appended list appears as a string. I have no clue how to split it into singular usernames like it appears in the list “compromised_users.”

Is there a command such as split ’ , ’ or (x, delimiter = ‘,’) that I can use to separate my data entries for the file? I guess a person interpreting strings could guess where to split the data but a list of floats and integers would be nearly impossible to decipher. The ‘Get Help’ video also produces a file identical to the one I have but it just doesn’t seem useful
SCRIPT:
import csv
compromised_users =
with open(‘passwords.csv’) as password_file:
password_csv = csv.DictReader(password_file)
for row in password_csv:
compromised_users.append(row[‘Username’])

with open(‘compromised_users.txt’, ‘w’) as compromised_user_file:
for user in compromised_users:
compromised_user_file.write(user)

FILE:
jean49haydenashleymichaelastephensdenisephillipsandrew24kaylaabbotttmartinezmholdenrandygilbertwatsonlouismdavispatrickpricekgriffithhannasarahxaviermartinhrodriguezerodriguezdanielleclarktimothy26elizabeth19

LIST:[‘jean49’, ‘haydenashley’, ‘michaelastephens’, ‘denisephillips’, ‘andrew24’, ‘kaylaabbott’, ‘tmartinez’, ‘mholden’, ‘randygilbert’, ‘watsonlouis’, ‘mdavis’, ‘patrickprice’, ‘kgriffith’, ‘hannasarah’, ‘xaviermartin’, ‘hrodriguez’, ‘erodriguez’, ‘danielleclark’, ‘timothy26’, ‘elizabeth19’]

1 Like

Is there a command such as split ’ , ’ or (x, delimiter = ‘,’) that I can use to separate my data entries for the file?

There sure is! It’s the String split() method. There should be a lot of documentation out there (it’s relatively friendly to use).

why, when writing the compromised_users.txt file, the appended list appears as a string

It’s an exercise. You’ll often have to manipulate data if you plan to do programming :slight_smile: Obviously, life would be easier if it came in our desired formats… but what would programmers do then?? Haha.

A real life example is csv files that are just that, strings separated by commas.

Earlier modules of Python in CC cover these techniques!

Thanks for the quick and informational response. I am obviously quite new to the programming world and can use all the help available.

I’ll look into split a bit more as well as how to maintain cohesive formatting across files.

Thanks again

1 Like

No problem! Feel free to ask questions here any time. There’s plenty of people pitching in to help out.

Good luck in your studies.

1 Like

@code6304873228
I came across the same issue. Didn’t like how the output was formatted in “compromised_users.txt”

Here’s what I did to solve it:


I got rid of the for loop and just used the .join() method with using a newline ‘\n’ as the delimiter. Could have put this inside the .write() but left it separate for clarity.

The new “compromised_users.txt” file formats like this:

Screen Shot 2020-07-09 at 5.52.06 PM
Note: cropped image to save space