How to read a text file into a string variable and strip newlines?

前端 未结 23 2161
醉酒成梦
醉酒成梦 2020-11-22 05:47

I use the following code segment to read a file in python:

with open (\"data.txt\", \"r\") as myfile:
    data=myfile.readlines()

Input fil

相关标签:
23条回答
  • 2020-11-22 06:01

    This works: Change your file to:

    LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE
    

    Then:

    file = open("file.txt")
    line = file.read()
    words = line.split()
    

    This creates a list named words that equals:

    ['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']
    

    That got rid of the "\n". To answer the part about the brackets getting in your way, just do this:

    for word in words: # Assuming words is the list above
        print word # Prints each word in file on a different line
    

    Or:

    print words[0] + ",", words[1] # Note that the "+" symbol indicates no spaces
    #The comma not in parentheses indicates a space
    

    This returns:

    LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN, GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE
    
    0 讨论(0)
  • 2020-11-22 06:02

    python3: Google "list comphrension" if the square bracket syntax is new to you.

     with open('data.txt') as f:
         lines = [ line.strip( ) for line in list(f) ]
    
    0 讨论(0)
  • 2020-11-22 06:04

    To remove line breaks using Python you can use replace function of a string.

    This example removes all 3 types of line breaks:

    my_string = open('lala.json').read()
    print(my_string)
    
    my_string = my_string.replace("\r","").replace("\n","")
    print(my_string)
    

    Example file is:

    {
      "lala": "lulu",
      "foo": "bar"
    }
    

    You can try it using this replay scenario:

    https://repl.it/repls/AnnualJointHardware

    0 讨论(0)
  • 2020-11-22 06:05

    I don't feel that anyone addressed the [ ] part of your question. When you read each line into your variable, because there were multiple lines before you replaced the \n with '' you ended up creating a list. If you have a variable of x and print it out just by

    x

    or print(x)

    or str(x)

    You will see the entire list with the brackets. If you call each element of the (array of sorts)

    x[0] then it omits the brackets. If you use the str() function you will see just the data and not the '' either. str(x[0])

    0 讨论(0)
  • 2020-11-22 06:05

    Try the following:

    with open('data.txt', 'r') as myfile:
        data = myfile.read()
    
        sentences = data.split('\\n')
        for sentence in sentences:
            print(sentence)
    

    Caution: It does not remove the \n. It is just for viewing the text as if there were no \n

    0 讨论(0)
  • 2020-11-22 06:07

    I'm surprised nobody mentioned splitlines() yet.

    with open ("data.txt", "r") as myfile:
        data = myfile.read().splitlines()
    

    Variable data is now a list that looks like this when printed:

    ['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']
    

    Note there are no newlines (\n).

    At that point, it sounds like you want to print back the lines to console, which you can achieve with a for loop:

    for line in data:
        print line
    
    0 讨论(0)
提交回复
热议问题