Python edit line from txt file - python

I'm making program that open txt file and replace first 0 with 1 of given line. Now it only print the edited line, but I want that it prints all the lines. I'm using python 3.1.
line_number = 3
with open(filename, "r") as f:
number = 0
for line in f:
number += 1
if line_number == number:
content = line.replace("0","1",1)
savefile = filename[:4] + ".tmp"
with open(savefile, "w") as f:
f.write(content)
os.remove(filename)
os.rename(savefile, filename)
Text file:
0 Dog
0 Cat
0 Giraffe
0 Leopard
0 Bear

You need to write each unchanged line to the savefile:
import os
filename = 'input.txt'
line_number = 3
savefile = filename[:4] + ".tmp"
with open(filename, "r") as f:
with open(savefile, "w") as fout:
number = 0
for line in f:
number += 1
if line_number == number:
content = line.replace("0","1",1)
fout.write(content)
else:
# Write unchanged lines here
fout.write(line)
os.remove(filename)
os.rename(savefile, filename)

Did you try something like this:
filename = "./test.txt"
with open(filename) as f:
lines = f.readlines()
# the element with index 2 is the 3-th element
lines[2] = lines[2].replace("0","1",1)
with open(filename, 'w') as f:
[f.write(line) for line in lines]
Output(./test.txt):
0 Dog
0 Cat
1 Giraffe
0 Leopard
0 Bear
You can read the file and save it to a list. Then you can then perform a certain action for each item(or for a specific element) in the list and save the result in the same file. You don't need of .tmp file or to remove and rename a file.
Edit:
There is an another approach with fileinput (thanks to #PeterWood)
import fileinput
with fileinput.input(files=('test.txt',), inplace=True) as f:
for line in f:
if fileinput.lineno() is 3:
print(line.replace("0", "1", 1).strip())
else:
print(line.strip())

Related

Python Code to Copy lines from One CSV to Another and Add Column

I'm trying to use Python to copy lines from one csv file to another and add data to a new column in the process. The data is being copied correctly to the new file, but it's all being copied to the same line in the new file.
file = "C:/original_file.csv"
nf = "C:/file_updated.csv"
i = 0
with open(file, 'r') as origFile:
with open(nf, 'w') as newFile:
lineList = []
for line in origFile:
strippedLine = line.strip()
lineList = strippedLine.split(',')
lineList.append("C:/ATT" + str(i) + "_PHOTO 1.jpg")
lineStr = str(lineList)
lineStr = lineStr.replace("'", "")
newFile.write(lineStr)
print lineList
i += 1
origFile.close()
newFile.close()
How can I make it so that each line from the first file copies to a separate line of the new file?
file = "C:/original_file.csv"
nf = "C:/file_updated.csv"
i = 0
with open(file, 'r') as origFile:
with open(nf, 'w') as newFile:
lineList = []
for line in origFile:
strippedLine = line.strip()
lineList = strippedLine.split(',')
lineList.append("C:/ATT" + str(i) + "_PHOTO 1.jpg")
lineStr = str(lineList)
lineStr = lineStr.replace("'", "")
newFile.write(lineStr)
newFile.write('\n') #Insert a new line
print lineList
i += 1
origFile.close()
newFile.close()
No need to install pandas, the built-in csv library is great for this!!
$ cat tmp.csv
first,second
third,fourth
import csv
to_read = "./tmp.csv"
to_write = "./tmp2.csv"
with open(to_read, newline="") as to_read_fp, open(to_write, "w", newline="") as to_write_fp:
reader = csv.reader(to_read_fp)
writer = csv.writer(to_write_fp)
for count, row in enumerate(reader):
row.append(f"C:/ATT{count}_PHOTO 1.jpg")
writer.writerow(row)
$ cat tmp2.csv
first,second,C:/ATT0_PHOTO 1.jpg
third,fourth,C:/ATT1_PHOTO 1.jpg
If you want to do it without any imports you could try something like this which adds a new column with the header New Field.
Of course it assumes the original CSV has a header row.
file = "original_file.csv"
nf = "file_updated.csv"
with open(file, 'r') as origFile:
data = [line.strip().split(',') for line in origFile.readlines()]
header = data[0]
data = data[1:]
header.append('New Field')
data = [line + [f'C:/ATT{idx}_PHOTO 1.jpg'] for idx, line in enumerate(data)]
data = [','.join(line) for line in [header]+data]
with open(nf, 'w') as newFile:
newFile.writelines('\n'.join(data))
"""
SAMPLE INPUT
Field1,Field2
Data1,Data2
Data3,Data4
SAMPLE OUTPUT
Field1,Field2,New Field
Data1,Data2,C:/ATT0_PHOTO 1.jpg
Data3,Data4,C:/ATT1_PHOTO 1.jpg
"""

How to remove a range of lines from csv file?

I Would like to remove lines 3 to 15 from file 'Database.csv'. Please see my code below, this code is works only for one line i added range, but is didn't work. ;/
filename = 'Database.csv'
line_to_delete = [3:15]
initial_line = 1
file_lines = {}
with open(filename) as f:
content = f.readlines()
for line in content:
file_lines[initial_line] = line.strip()
initial_line += 1
f = open(filename, "w")
for line_number, line_content in file_lines.items():
if line_number != line_to_delete:
f.write('{}\n'.format(line_content))
f.close()
print('Deleted line: {}'.format(line_to_delete))
You can use csv library for doing this.
import csv
file=open("Database.csv",'rb')
final_file=open("Database_edited",'wb')
writer=csv.writer(final_file)
line_no=1 # for knowing the line number
for row in csv.reader(file):
if(line_no<=3 or line_no>=15):
writer.writerow(row)
line_no=line_no+1
file.close()
final_file.close()
This way Database_edited will have your required file
Here's an easy way to do using the csv module and the range() function:
mport csv
filename = 'Database.csv'
startline, endline = 3, 15 # Inclusive.
with open(filename, 'r', newline='') as f:
content = [row for i,row in enumerate(csv.reader(f), 1)
if i not in range(startline, endline+1)]
filename2 = 'Database2.csv' # Write to different file for testing.
with open(filename2, 'w', newline='') as f:
csv.writer(f).writerows(content)
print('lines deleted')
#Tested code on Python 3.6
import csv
filename = r'C:\Users\91852\Desktop\New folder (2)\lines.csv'
start_skip_row = 3
stop_skip_row = 13
def readCSVFile(file_name: str, start: int, stop: int) -> list:
with open(file_name, 'r') as file_content:
data_after_deletion = [line.replace('\n', '') for idx, line in enumerate(file_content) if
not start - 1 <= idx < stop]
print(data_after_deletion)
return data_after_deletion
def writeCSVFile(data: list) -> None:
with open('file_after_deletion', 'w', newline='') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(data)
data_after_deletion = readCSVFile(filename, start_skip_row, stop_skip_row)
writeCSVFile(data_after_deletion)

Removing complete lines of text in a text file (on Python)

I am working with an output log file that has 12 thousand lines of code, most of which include something that looks like this:
"760.0132 EXP window1: blendMode = 'avg'"
My goal is to entirely remove any line that has "EXP window1: blendMode = 'avg'". I can remove that text bit from all of the lines where it is found, but not the number. This is the code I have used to delete the text bits (borrowed from another stack overflow question/answer):
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_list = [" EXP window1: blendMode = 'avg'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
I was hoping that I would be able to add something like
delete_list = ["1**.**** EXP window1: blendMode = 'avg'"]
in order to delete any number that includes all of the text, and also any number in that line, but it does not seem to work. Any advice on how to best clean up the log file would be greatly appreciated.
Thanks very much,
Simon
Why do you want to do this using Python? You can do this with a simple grep -v or findstr /V, as in following example:
Prompt>grep -v "blendmode" input.txt >output.txt
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_list = [" EXP window1: blendMode = 'avg'"]
fin = open(infile)
fout = open(outfile, "a")
for line in fin:
for word in delete_list:
if word in line:
wordCheck = False
break
else:
wordCheck = True
if wordCheck:
fout.write(line)
fin.close()
fout.close()
Maybe cleaner:
with open("01_Day1_run1.txt", "r") as infile, open("01_Day1_run1_cleaned.txt", "a") as outfile:
for line in infile:
if not any(filter in line for filter in delete_list ):
outfile.write(line)
infile = "01_Day1_run1.txt"
outfile = "01_Day1_run1_cleaned.txt"
delete_string = "EXP window1: blendMode = 'avg'"
fin = open(infile)
fout = open(outfile, "a")
for line in fin.readLines():
if delete_list not in line:
fout.write(line)
fin.close()
fout.close()

Create separate output files

Here is sample code to show that I'm running these commands again. I'm guessing this is the wrong way to do this.
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
for line in infile:
if re.search(r"\b1000\b", line, flags=re.IGNORECASE):
count1 += 1
fh.write("{}, ".format(count1))
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
for line in infile:
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count2 += 1
fh.write("{}, ".format(count2))
I hope I have understood your idea correctly. I suggest you could do the following.
fh = open("my.csv", "w+")
path = 'my path'
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
count1 = 0
count2 = 0
for line in infile:
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count1 += 1
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count2 += 1
# write the count value to the csv after reading the file
fh.write("Count1: {} Count2: {}\n".format(count1, count2))
# close the file reader - otherwise there won't be any content in the csv
fh.close()
You can do :
fh = open("output.csv", "w")
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
for line in infile:
if re.search(r"\b10G\b", line, flags=re.IGNORECASE):
count1 += 1
fh.write("{}, ".format(count1))

Writing on the topmost row of a CSV file

I have this sample.csv file:
a 1 apple
b 2 banana
c 3 cranberry
d 4 durian
e 5 eggplant
And have the following code:
samplefile = open(sample.csv,'rb')
rows = samplefile .readlines()
outputfile = open(output.csv,'wb')
wr = csv.writer(outputfile)
for row in rows:
wr.writerow(row)
What I wanted to to is write on the first row of outputfile at some point during the for loop, i.e. when outputfile may already have entries.
If you want to add to the end of the file(append to it):
with open("sample.csv", "a") as fp:
fp.write("new text")
If you want to overwrite the file:
with open("sample.csv", "w") as fp:
fp.write("new text")
If you want to remove a line from file:
import fileinput
import sys
for line_number, line in enumerate(fileinput.input('myFile', inplace=1)):
if line_number == 0: #first line
continue
else:
sys.stdout.write(line)
If you want to add a new first line(before the existing one):
with open("sample.csv", "r+") as fp:
existing=fp.read()
fp.seek(0) #point to first line
fp.write("new text"+existing) # add a line above the previously exiting first line

Categories