How to print select lines of a text file in python? - python-3.x

I have a text file with multiple lines of text. Each line of text is split into two columns, separated by a comma. How do I write a program to print only the lines of the text file that have a specific value for the first column? So for example how do I write a program to print every line that has "hello" as the first column?
I'm using python 3.3.3

#!/usr/bin/env python3
import sys
filename = sys.argv[1]
# read the file line by line
with open(filename) as f:
for line in f:
# split the line
columns = line.split(",")
# print all lines with "hello" as the first column
if columns[0] == "hello":
print(line, end='')

Related

How would one split line into an array of words and convert them to lowercase with Python?

How would one split line into an array of words and convert them to lowercase with Python? I am working with a TXT file. Below is my work thus far:
file_data = []
# ------------ Add your code below --------------
# We need to open the file
with open('/dsa/data/all_datasets/hamilton-federalist-548.txt', 'r') as file:
# For each line in file
for line in file:
line = line.strip()
split_line = line.split(' ')
file_data.append(split_line)
print(split_line)
# We want to split that line into an array of words and convert them to lowercase
# [x.lower() for x in ["A","B","C"]] this example code will covert that list of letters to lowercase
print(file_data.lower())
You have to convert them before adding them to the file_data list.
So instead of:
split_line = line.split(' ')
Try this:
split_line = [i.lower() for i in line.split(' ')]

python3 replace a empty line with fileinput

I have a file that has empty line between each line like this:
line one
line two
line three
I want to append a line after line two
This is how I am trying to do it
for line_num, line in enumerate(fileinput.FileInput(file, inplace=1) ):
if line_num == 4:
line.replace(" ", 'line to append')
The problem with this is it overwrites the entire file when I want to append the line
Your approach has a few (small) issues.
you do not print the lines to the file
line_num == 4 will not write after line two, unless you use enumerate with start=1
(assuming that you want to fill an empty line) line.replace(' ', 'line to append') will not do the trick because an empty line does contain a space
Try:
>>> import fileinput
>>> with fileinput.input('test', inplace=True) as f:
... for line_num, line in enumerate(f):
... if line_num == 3 and line in ['\n', '\r\n']:
... line = 'line to append\n'
... print(line, end='')
...
test had originally the content in your description and after:
$ cat test
line one
line two
line to append
line three
line_num == 3 and line in ['\n', '\r\n'] says "find the 4th line and check if it is empty". You may want to update/replace the checks based on your needs.

How to delete or skip a list of lines in a text file and print the remaining lines in a new text file?

I am very new to python. I am trying to create a script that prints lines of text to a text file that exclude a list of lines. Is the error IndexError : List index out of range due to the .pop function?
with open(file_path) as f:
lines = []
lines = open(f,'r').readlines()
# delete the following lines from the textfile
skip_line =[14,27,39,56,78]
while skip_line:
pop = skip_line.pop(0)
print(pop)
print(lines[pop])
lines.remove(lines[pop])
with open('duplicates_removed.txt', 'w') as savefile:
savefile.writelines(lines)
savefile.close()
I expect that the lines found in lines[pop] will be removed from lines.
Actual result:
IndexError : List index out of range
skip_lines = {14, 27, 39, 56, 78}
with open(filepath) as infile:
with open("duplicates_removed.txt", "w") as outfile:
for index, line in enumerate(infile):
if index not in skip_lines:
outfile.write(line)

There is a problem in conversion of text file content into csv format using python

I tried to convert text file content into a .csv format by reading each and every line using python csv module and converting that to a list. But i couldn't get the expected output and it stores the first line in a row but second line will be stored in 3rd row and 5th so on. Since I am new to python i don't know how to skip the line and store it in the right order.
def FileConversion():
try:
with open('TextToCSV.txt', 'r') as textFile:
LineStripped = (eachLine.strip() for eachLine in textFile)
lines = (eachLine.split(" ") for eachLine in LineStripped if eachLine)
with open('finalReport.csv', 'w') as CSVFile:
writer = csv.writer(CSVFile)
writer.writerow(('firstName', 'secondName', 'designation', "age"))
writer.writerows(lines)
Why don't you try doing something more simple:
import pandas as pd
aux = pd.read_csv("TextToCSV.txt", sep=" ")
aux.columns=['firstName', 'secondName', 'designation', "age"]
aux.to_csv("result.csv")

Writing python scripts

I need to write a standalone program that would run on a python cmd. This program counts the number of characters in every line of HumptyDumpty.txt file, and outputs this to a new file.
Note that the new file needs to contain only the number of characters per line.
Here's my code:
import sys
infilename = sys.argv[1]
outfilename = sys.argv[2]
infile=open(infilename)
outfile=open(outfilename, 'w')
char_=0
for line in infile:
line.split()
char_= len(line.strip("\n"))
outfile.write(str(char_ ))
print(line,end='')
infile.close()
outfile.close()
The ouput file has only one line, the concatenation of xyz instead of
x
y
z
"\n" doesnt seem to be doing the trick. Any suggestions?
If you don't want to include the white space between the words then you should replace them with an empty string.
for line in infile:
nline = line.replace(" ", "")
nline = nline.strip("\n")
char= len(nline)
outfile.write(str(char))
outfile.write("\n")
print(line, end='')
print(char)

Resources