Printing a Chosen Line from a File [closed] - python

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
How can I use Python to print a line of my choosing from a file?
E.g. a file 10 lines long and I use sys.argv to provide a value for the line that I wish to print.

f = open('put_your_text_file.csv', 'r')
num = sys.argv[2]
for i, line in enumerate(f.readlines()):
if i == num:
print(line)

Related

Print the same word 3 times on the same line Python [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 1 year ago.
Improve this question
I want to print out 3 of the same words on the same line after reading it from a file. So say the word is Test. I want it to print TestTestTest. Here is the code I came up with, but it prints each as a separate line. How do I fix this?
import os
file = open('rockyou.txt', 'r')
for line in file:
output = line.title()
print(output+output+output)
It must have newlines:
for line in file:
output = line.strip().title()
print(output + output + output)
Instead of output + output + output, just do multiplication:
for line in file:
output = line.strip().title()
print(output * 3)

Data extract from text 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 6 years ago.
Improve this question
Please help.
I have data in text file with 2 long columns tab spaced
I have to extract first and second columns separately
Can it be done with Python ?
If you need the columns as two separate lists:
first_column = []
second_column = []
data_file = open('your_file.txt')
for line in data_file.readlines():
first_column.append(line.split('\t')[0])
second_column.append(line.split('\t')[1])
data_file.close()

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: 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