Appending to the end of a certain line - python

Rather than appending to the end of a file, I am trying to append to the end of a certain line of a .csv file.
I want to do this when the user enters an input that matches the first column of the .csv.
Here's an example:
file=open("class"+classno+".csv", "r+")
writer=csv.writer(file)
data=csv.reader(file)
for row in data:
if input == row[0]:
(APPEND variable TO ROW)
file.close()
Is there a way to do this? Would I have to redefine and then rewrite the file?

You can read the whole file then change what you need to change and write it back to file (it's not really writing back when it's complete overwriting).
Maybe this example will help:
read_data = []
with open('test.csv', 'r') as f:
for line in f:
read_data.append(line)
with open('test.csv', 'w') as f:
for line in read_data:
key,value = line.split(',')
new_line = line
if key == 'b':
value = value.strip() + 'added\n'
new_line = ','.join([key,value])
f.write(new_line)
My test.csv file at start:
key,value
a,1
b,2
c,3
d,4
And after I run that sample code:
key,value
a,1
b,2added
c,3
d,4
It's probably not the best solution with big files.

Related

Want to append a column in a file without using the Pandas

I have a file say, outfile.txt which looks like below:
1,2,3,4,0,0.95
1,2,4,4,0,0.81
5,6,3,1,0,0.89
7,6,8,8,0,0.77
6,6,4,9,0,0.88
9,9,9,1,0,0.66
4,3,6,9,0,0.85
1,2,6,7,0,0.61
Now I want to append one extra 1 to each row. So the desired output file looks like:
1,2,3,4,0,0.95,1
1,2,4,4,0,0.81,1
5,6,3,1,0,0.89,1
7,6,8,8,0,0.77,1
6,6,4,9,0,0.88,1
9,9,9,1,0,0.66,1
4,3,6,9,0,0.85,1
1,2,6,7,0,0.61,1
How can I do it? Whenever I am googling it to find a solution, I am seeing everywhere this kind of solution is provided using Pandas, But I don't want to use that.
Since your file is in csv format, csv module can help you. If you iterate over the reader object, it gives you a list of the items in each line in the file, then simply .append() what you want.
import csv
with open("outfile.txt") as f:
reader = csv.reader(f)
for line in reader:
line.append("1")
print(",".join(line))
If you have a column like column you can zip it with the reader object and append the corresponding element in the loop:
import csv
column = range(10)
with open("outfile.txt") as f:
reader = csv.reader(f)
for line, n in zip(reader, map(str, column)):
line.append(n)
print(",".join(line))
I printed, you can write it to a new file.
You can read and write files line by line with the csv module. A reader object will iterate the rows of the input file and writer.writerows will consume that iterator. You just need a bit of extra code to add the 1. Using a list generator, this example adds the extra column.
import csv
import os
filename = "outfile.txt"
tmp = filename + ".tmp"
with open(filename, newline="") as infile, open(tmp, "w", newline="") as outfile:
csv.writer(outfile).writerows(row + [1] for row in csv.reader(infile))
os.rename(tmp, filename)
Just, iterate through the file line by line and add ,1 at the end of each line:
with open('outfile.txt', 'r') as input:
with open('outfile_final.txt', 'w') as output:
for line in input:
line = line.rstrip('\n') + ',1'
print(line, file=output)

Splitting CSV into files

I am looking for some guidance with what I am trying to do.
I have a .csv file, and in this file I want to break down each line and save it into its own text file.
I have that part working, however, when it runs I am losing the commas. I am assuming this is happening because I am converting a .csv file into a list then into a text file.
I feel there has to be a better way!
Code
def createParam():
with open('testcsv.csv', 'r') as f:
reader = csv.reader(f)
csvList = list(reader)
for item in csvList:
os.mkdir(r"C:\Users\user\Desktop\Test Path\\" + item[0])
f=open(r"C:\Users\user\Desktop\Test Path\\" + item[0] + r"\prm.263","w+")
f.writelines(item)
f.close
CSV
Store1,1080,SafehavenHumaneSociety,2904,LuckyPaws,3156,StMartinsDogRescue,4051,SalemFriendsofFelines,4088,HeartlandHumaneSociety,4118,Fortheloveofacat,6329,PeacefulPack,7710,OneVoice4Paws,7981,KeithasKittieRescue,7984,InternationalReptileRescueInc,9304,SeniorDogRescueOfOregon,9309,LovedAgainPets
Store2,0028,ArizonaAnimalWelfareLeague,0039,HelpingAnimalsLiveOnHALO,1468,MaricopaCountyAnimalCareandControlMCACC,4250,BuckeyeAnimalRescueKennel,5112,MASH,5957,FeathersFoundationInc,6725,ValleyHumaneSociety,7172,KitKatRescue,7627,LuckyDogRscu,7761,AZSmallDog,8114,WhoSavedWhoRescue,9160,DestinationHome,9248,AllAboutAnimals
Clarification: When it creates the file(s), it has all the data, but all the commas are removed so its just all 1 long line.
Since each item is a list of values representing a row in the CSV, you should write it as a CSV with csv.writer:
for item in csvList:
os.mkdir(r"C:\Users\user\Desktop\Test Path\\" + item[0])
with open(r"C:\Users\user\Desktop\Test Path\\" + item[0] + r"\prm.263","w+") as f:
csv.writer(f).writerow(item[1:])
I guess you just need to load the file and read line by line (not loading it as a csv file). Each line goes to a file.
index = 0
with open('testcsv.csv', 'r') as f:
for line in f.readlines():
index += 1
with open('new_textfile_{}.csv'.format(index), 'w') as f2:
f2.write(line)
If you want to save the files in some directory X, then the path in the second with open... should be "X/whatever_name_{}.csv".format(index)

how to .writerow() to only 1 row in csv file?

Currently in my code it changes the 3rd row but for all rows, I want it to only change the row with the entered GTIN by the user.
Current code:
file=open("stock.csv")
stockfile= csv.reader(file)
for line in stockfile:
if GTIN in line:
currentstock= line[2]
targetstock = line[3]
newstock = (int(currentstock) - int(Quantity))
currentstock = str(currentstock)
targetstock = str(targetstock)
newstock = str(newstock)
if newstock < targetstock :
import csv
reader = csv.reader(open('stock.csv',"r"))
new = csv.writer(open('out.csv',"w"))
for line in reader:
new.writerow([line[0], line[1], newstock , line[3]])
Output in file (it changes all numbers in 3rd column):
86947367,banana,1,40
78364721,apple,1,20
35619833,orange,1,30
84716491,sweets,1,90
46389121,chicken,1,10
How can I only change the row with the GTIN the user enters?
use the csv module:
https://docs.python.org/3/library/csv.html
It has a csv.reader() and csv.writer(). Read the file into memory, iterate over it doing calcs/replacements, then write each row to a new list. Finally, generate a new data file to replace the old one.
I answered one of your other questions before you were using csvreader but it looks like it got deleted. But the principle is the same. As I stated in one of the comments, I don't think you should keep reopening/rereading stock.txt. Just read it line by line then write line by line to an output file:
stock_number = input('Enter the stock number: ')
new_item = input('Enter item to add to above stock listing: ')
lines = []
with open('stock.txt', 'r') as infile:
for line in infile:
lines.append(line)
# can call this 'output.txt' if you don't want to overwrite original
with open('stock.txt', 'w') as outfile:
for line in lines:
if stock_number in line:
# strip newline, add new item, add newline
line = '{},{}\n'.format(line.strip(), new_item)
outfile.write(line)
Edit: here it is with csv module instead. This makes it a little more straightforward because the csv module gives you a list of strings for each line, then you can add to or modify them as desired. Then you can just write the list back line by line, without worrying about newlines or delimiters.
import csv
stock_number = input('Enter the stock number: ')
new_item = input('Enter item to add to above stock listing: ')
lines = []
with open('stock.txt', 'r') as infile:
for line in csv.reader(infile):
lines.append(line)
# change this to 'stock.txt' to overwrite original file
with open('output.txt', 'w') as outfile:
writer = csv.writer(outfile)
for line in lines:
if stock_number in line:
line.append(new_item)
writer.writerow(line)
Also you shouldn't really import anything in the middle of the code like that. Imports generally go at the top of your file.

search contents of one file with contents of a second file using python

I have the following code which compares the items on the first column of input file1 with the contents of input file 2:
import os
newfile2=[]
outfile=open("outFile.txt","w")
infile1=open("infile1.txt", "r")
infile2=open("infile2.txt","r")
for file1 in infile1:
#print file1
file1=str(file1).strip().split("\t")
print file1[0]
for file2 in infile2:
if file2 == file1[0]:
outfile.write(file2.replace(file2,file1[1]))
else:
outfile.write(file2)
input file 1:
Modex_xxR_SL1344_3920 Modex_sseE_SL1344_3920
Modex_seA_hemN Modex_polA_SGR222_3950
Modex_GF2333_3962_SL1344_3966 Modex_ertd_wedS
input file 2:
Sardes_xxR_SL1344_4567
Modex_seA_hemN
MOdex_uui_gytI
Since the input file 1 item (column 1, row 2) matches an item in input file 2 (row 2), then the column 2 item in input file 1 replaces the input file 2 item in the output file as follows (required output):
Sardes_xxR_SL1344_4567
Modex_polA_SGR222_3950
MOdex_uui_gytI
So far my code is only outputting the items in input file 1. Can someone help modify this code. Thanks
Looks like you have a tsv file, so let's go ahead and treat it as such. We'll build a tsv reader csv.reader(fileobj, delimiter="\t") that will iterate through infile1 and build a translation dict from it. The dictionary will have keys of the first column and values of the second column per row.
Then using dict.get we can translate the line from infile2 if it exists in our translation dict, or just write the line itself if there's no translation available.
import csv
with open("infile1.txt", 'r') as infile1,\
open('infile2.txt', 'r') as infile2,\
open('outfile.txt', 'w') as outfile:
trans_dict = dict(csv.reader(infile1, delimiter="\t"))
for line in infile2:
outfile.write(trans_dict.get(line.strip(),line.strip()) + "\n")
Result:
# contents of outfile.txt
Sardes_xxR_SL1344_4567
Modex_polA_SGR222_3950
MOdex_uui_gytI
EDIT as per your comment:
import csv
with open("infile1.txt", 'r') as infile1:
# build our translation dict
trans_dict = dict(csv.reader(infile1, delimiter="\t"))
with open("infile2.txt", 'r') as infile2,\
open("outfile.txt", 'w') as outfile:
# open the file to translate and our output file
reader = csv.reader(infile2, delimiter="\t")
# treat our file to translate like a tsv file instead of flat text
for line in reader:
outfile.write("\t".join([trans_dict.get(col, col) for col in line] + "\n"))
# map each column from trans_dict, writing the whole row
# back re-tab-delimited with a trailing newline

Start reading and writing on specific line on CSV with Python

I have a CSV file that looks like this:
COL_A,COL_B
12345,A=1$B=2$C=3$
How do I read that file and wrote it back to a new file but just the second row (line)?
I want the output file to contain:
12345,A=1$B=2$C=3$
Thanks!
The following reads your csv, extracts the second row, then writes that second row to another file.
with open('file.csv') as file:
second_line = list(file)[1]
with open('out.csv', mode = 'w') as file:
file.write(second_line)
outfile = open(outfilename,"w")
with open(filename) as f:
for line in f:
print >> outfile , line.split()[-1]
outfile.close()
as long as the lines actually look like the line you posted in the OP

Categories