Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:29:09+00:00 2026-06-17T08:29:09+00:00

I have 2 lists of strings, they are constructed from database content but I

  • 0

I have 2 lists of strings, they are constructed from database content but I have difficulty in reading it into a txt file

list_a={u'XYZ-123-456',u'XYZ-123-456',u'XYZ-789-456',....}

list_b={u'Tom \n is creative', u'Sky is blue',....}

What I need to do is get rid of ‘u’ at the beginning of each string in both lists, getting rid of \n within each string in list_b where applicable.

then write each element in list_b to a text file, one by a line, into the txt file, whose title is the element in list_a. Strings with the same title should be in the same txt file. Anyone can help ?

I think I can use pop function to get rid of u, but how about the ‘\n’ within the string?
I am also not 100% sure about the I/O function. Anyone can help me out with the sample script?

Thanks a lot, a newbie of python

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:29:11+00:00Added an answer on June 17, 2026 at 8:29 am

    Welcome so SO.

    list_a is actually a set. The {} indicates set, which is a list of unique items – so you may not want this type if you expect multiple values in the ‘list’. If you want an actual list then encapsulate the ‘list’ with [...] instead of {...}

    The ‘u’ in the set indicates the text is unicode. if you want this removed:

    list_a = map(str, list_a)
    

    \n is a control character indicating a new line. This will be represented as a new line when printed in the console. If you really don’t want new lines then do:

    list_b= { item.replace('\n','') for item in list_b }
    

    I assume you want each item on a new line in the text file. If so:

    with open('c:\file.txt', 'w') as file:
        file.write('\n'.join(list_a)) 
        file.write('\n'.join(list_b)) 
    

    for non duplicates merge the 2 lists into a set. As they are currently already sets this will work:

    with open('c:\file.txt', 'w') as file:
        file.write('\n'.join(list_a + list_b)) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just some example code here, but I have lists of strings that I want
I have used linked lists before with Strings, doubles, etc., and they always worked
I have lists where strings correspond with values. For example, in a company list
I have about 50 million lists of strings in Python like this one: [1,
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();
I am passing in some lists from a view into a django template. In
I have a list of file locations stored as strings. I want to be
I have some code here which reads from a file, and stores them in
I have a config file that contains a list of strings. I need to
I have a list of words (assume they are stored in String[] if you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.