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)
Related
I'm trying to replace the first line of a csv input file with a header. The first line is blank, 0. See image below.
I want the blank and 0 to be "ID" and "sil_score" respectively. See below:
But I keep getting this:
import csv
r = csv.reader(open('C:/Users/Desktop/Geosill/attempt1.csv'))
lines = list(r)
lines[1][0] = 'ID'
lines[2][0] = 'sil_score'
writer = csv.writer(open('C:/Users/Desktop/Geosill/attempt3.csv', 'w'))
writer.writerows(lines)
This will do it. newline='' should be used to fix the blank line issue you are seeing as well.
import csv
with open('input.csv',newline='') as f:
r = csv.reader(f)
lines = list(r)
lines[0] = ['ID','sil_score']
with open('output.csv','w',newline='') as f:
w = csv.writer(f)
w.writerows(lines)
If you're looking to edit the first two lines of the .csv you'll have to change how you access the lines list.
You'll need to use
lines[0][0]='ID'
lines[0][1]='sil_score'
instead.
The output seems odd though, could be something weird with the csv import. Try opening the files in a text editor, might be easier to see what's going on.
You can do this without using csv.writer. Try this:
with open('C:/Users/Desktop/Geosill/attempt1.csv', "r") as infile:
lines = infile.readlines().rstrip().split(",")
lines[0] = ["ID", "sil_score"]
with open('C:/Users/Desktop/Geosill/attempt1.csv', "w") as outfile:
for line in lines:
outfile.write(",".join(line))
Hope this helps!
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)
I have this input file:
one\tone
two\ttwo
three\tthree
With a tab between each word.
I am trying to save it in a csv file where each word ends up in its own cell. This is my code:
import csv
input = open('input.txt').read()
lines = input.split('\n')
with open('output.csv', 'w') as f:
writer = csv.writer(f)
for line in lines:
writer.writerow([line])
However, both words end up in the same cell:
How do I change the code so that each word ends up in its own cell?
Try this:
import csv
input = open('input.txt').read()
lines = input.split('\n')
with open('output.csv', 'w') as f:
writer = csv.writer(f)
for line in lines:
writer.writerow(line.split('\t'))
The writerow method in the CSV writer library takes a list of columns.
Currently, you are providing your whole string the value of the first column
writer.writerow([line])
Instead, try splitting the string by \t, thus creating a list of each individual word and provide that to the library instead.
writer.writerow(line.split("\t"))
You need to split the input lines into a list, so that csv.writer() will put them into seperate columns. Try:
with open('output.csv', 'w') as f:
writer = csv.writer(f)
for line in lines:
writer.writerow(line.split('\t'))
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
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