About 1,000,000 results
Open links in new tab
  1. How should I read a file line-by-line in Python? - Stack Overflow

    Jul 19, 2012 · If iteration closed the file, I wouldn't be able to do that. So keeping iteration and resource management separate makes it easier to compose chunks of code into a larger, working Python program. Composability is one of the most important usability features of a …

  2. python - How can I read a text file into a string variable and strip ...

    Oct 18, 2016 · Do you really want to read the entire text into one string variable? Do you really mean with "strip newlines" to replace them with an empty string? This would mean, that the last word of a line and the first word of the next line are joined and not separated. I don't know your use case, but this seems to be a strange requirement.

  3. python - How to read a file line-by-line into a list? - Stack Overflow

    How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list.

  4. Easiest way to read/write a file's content in Python

    Jul 5, 2019 · In Ruby you can read from a file using s = File.read (filename). The shortest and clearest I know in Python is with open (filename) as f: s = f.read () Is there any other way to do it that makes it

  5. How to read a text file into a list or an array with Python

    Feb 4, 2013 · I am trying to read the lines of a text file into a list or array in python. I just need to be able to individually access any item in the list or array after it is created. The text file is form...

  6. python - How do I read text files within a zip file? - Stack Overflow

    Oct 21, 2016 · Since Python 3.8, it's been possible to construct Path objects for zipfile contents, and use their read_text method to read them as text. Since Python 3.9 it's been possible to specify text mode in the path object's open method. import zipfile with zipfile.ZipFile('spam.zip') as zf: # Create a path object.

  7. python reading text file - Stack Overflow

    Feb 26, 2014 · I have a text file, of which i need each column, preferably into a dictionary or list, the format is : N ID REMAIN VERS 2 2343333 bana twelve 3 3549287 m...

  8. Read text file and parse in python - Stack Overflow

    Jul 15, 2018 · Hi, Sorry for the late reply. I found hygull and cosmic_inquiry's answers are useful. Couldn't try the below answers though. Their answers are also seem reasonable, too. If you are willing to try them, it would be very great if you update me the results. Very thanks and have a wonderful day :)

  9. Reading specific columns from a text file in python

    Jun 20, 2001 · I have a text file which contains a table comprised of numbers e.g: 5 10 6 6 20 1 7 30 4 8 40 3 9 23 1 4 13 6 if for example I want the numbers contained only in the second column, how do i extract

  10. How do I print the content of a .txt file in Python?

    Aug 15, 2013 · How to read and print the content of a txt file Assume you got a file called file.txt that you want to read in a program and the content is this: this is the content of the file with open you can read it and then with a loop you can print it on the screen. Using enconding='utf-8' you avoid some strange convertions of caracters. With strip(), you avoid printing an empty line between each (not ...

Refresh