Exclude spaces when reading a file [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am getting into file handling in python and want to read the info in a file. How do I read the file without additional lines?
Here's my code:
data = f.readlines()
for line in data:
print(line)

Incorporating falsetru's point,
for line in f:
if line.strip():
print(line)
The strip command will strip all white space, and at that point, if you have an empty string, the condition will fail.

Related

how to write regex to get only the users from this string output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
consider below the output of my subprocess module of python. Now from here I want to grab the very first username only like root ,daemon and continuew. I tried but not able to write the exact regex to fetch the users only .
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
You do not a regex in the first place, as per your comments. So, you may iterate over the output line by line (str.splitlines()), split the line with : (str.split(':')) and take the first result (result[0]). This expects the output to be consistent, else it will fail.
Use split instead of regex
for line in output.split('\n'):
print line.split(':')[0]
>>>root
>>>daemon
....

How to remove words from text file in Python that contain certain letter? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to Python and need to remove lines from text file in Python that contain a certain letter. i.e. a text file containing the months of the year and I want to remove the months containing an r. I'm completely lost.
How about this? (Reads from stdin, writes to stdout.)
import sys
for line in sys.stdin:
if 'r' not in line:
print(line)
Sample usage:
$ python program.py < months_file > months_without_r_file

Python search .txt file for specific line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Basically I want a Python script to search a .txt for any line containing,
" #1111. "
1111 < = any number from 0-9, so any possibility 0-9 with 4 numbers, containing # at the start and . at the end.
You'll want to use what's called a Regular Expression.
Python has a regular expression module called re.
import re
with open('file.txt', 'r') as f:
matches = [line for line in f if re.search(r'#\d{4}\.', line)]
print matches

Python: getting info from file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Users are prompted for the location of a file that contains multiple sets of some information: the product number, the price of the product, the day, the month, the year.
eg.
12345678,200,1,1,2014
23456789,150,1,1,2014
12345678,180,1,2,2014
I need to get the total number of prices (ie 200,150,180). How do I do so?
Open your file, iterate over its lines with a for loop, split each line by commas and print second field.
This should work:
filepath = raw_input('Input file path:\n')
with open(filepath) as f:
for line in f:
print line.split(',')[1]

Find string in a file and print which line it was found on [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I find a string in a text file and then print which line that string was found on?
I am using Python 2.7.
In Python, enumerate is handy for this sort of thing:
with open(myfile) as f:
for index, line in enumerate(f, 1):
if s in line:
print("'{0}' found on line {1}".format(s, index))

Categories

Resources