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
Related
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
"""
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())
I need to read from 3 txt files and merge them into one big txt file.
Ex text file1:
John
Mary
Joe
Ex text file2:
Alabama
Alaska
Michigan
Ex text file3:
Maybe
Attending
Not Attending
I'm not sure what else to add to my code
path = '/home/pi/Documents/Test/name.txt'
file1 = open (path, 'r')
name = file1.read()
statepath = '/home/pi/Documents/Test/state.txt'
file2 = open (path, 'r')
states = file2.read()
statuspath = '/home/pi/Documents/Test/status.txt'
file3 = open(statuspath, 'r')
status = file3.read()
finalpath = '/home/pi/Documents/Test/final.txt'
file4 = open(finalpath, 'w')
final = file4.read()
for item in name, states, status:
final.write(file1, "\n")
final.write(file2, "\n")
final.write(file3, "\n")
file1.close()
file2.close()
file3.close()
final.close()
final expected output of the file is
John <------- first value in file1
Alabama <------ first value in file2
Maybe <------- first value in file 3
Mary <---------- second value in file 1
Alaska
Attending
Joe
Michigan
Not Attending
Basically trying to loop through all of them and print them sequentially
not sure how to loop.
First of all you are writing in final without actually ever reading anything so it can't work. Replace file1, file2, file3 with the variables that have the read() attribute.
Just use a for statement with each variable you want to loop. Like this:
for i in name:
for j in states:
for k in status:
all = i + '\n` + j + '\n' + k + '\n'
final.write(all)
One of possible solution, but you should be sure that you have the same length of 3 files.
def main():
name_path = 'name.txt'
state_path = 'state.txt'
status_path = 'status.txt'
final_path = 'final.txt'
with open(name_path, 'r') as file1, open(state_path, 'r') as file2, open(status_path, 'r') as file3, open(final_path, 'w') as final:
for line in file1.readlines():
final.write(line)
final.write(file2.readline())
final.write(file3.readline())
Some way of doing this for a general case, using itertools:
import itertools as it
files = [
'/home/pi/Documents/Test/name.txt',
'/home/pi/Documents/Test/state.txt',
'/home/pi/Documents/Test/status.txt'
]
def loadData(fpath):
with open(fpath, "r") as f:
yield from f.redlines()
with open('/home/pi/Documents/Test/final.txt') as f:
for e in it.chain.from_iterable(zip(*map(loadDAta, files))):
f.write(e)
I just slightly improved Netwave version and it seems to be the right pythonic way to solver this task, the full code will be something like this
import itertools as it
def load_data(fpath):
with open(fpath, 'r') as f:
for line in f.readlines():
yield line
def main():
files = [
'/home/pi/Documents/Test/name.txt',
'/home/pi/Documents/Test/state.txt',
'/home/pi/Documents/Test/status.txt'
]
with open('/home/pi/Documents/Test/final.txt', 'w') as f:
for e in it.chain.from_iterable(zip(*map(load_data, files))):
for line in e:
f.write(line)
if __name__ == '__main__':
main()
I have a big csv file. After some items there is a newline character which is not supposed to be there. It is always after a specific item, let's say it's called 'foo'. I need to remove every newline character after foo. I figured out this is kind of what should happen:
for line in sys.stdin:
if line.split(",")[-1] == "foo":
line = line.rstrip()
How do I make sure I output the result back to the file?
You can't write line back to your original file but assuming you will use your script like python script.py < input_file.csv > output_file.csv you can simply print the lines you need:
import sys
for line in sys.stdin:
if line.split(",")[-1] == "foo":
line = line.rstrip()
# print() will append '\n' by default - we prevent it
print(line, end='')
I haven't tested this, but it should do what you need it to. This assumes there are no other items (other than foo) that has trailing white space that you don't want to strip. Otherwise, a simple conditional will fix that.
import csv
with open("/path/to/file", newline='') as f:
reader = csv.reader(f)
for row in reader:
for i, item in enumerate(row):
row[i] = item.rstrip()
with open("/path/to/file", 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(reader)
This answer just saves to a new csv file.
with open("test.csv", "r", newline="") as csvfile:
my_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
with open("new.csv", "w", newline="") as csvfile2:
last_line = []
writer = csv.writer(csvfile2, delimiter=',', quotechar='"')
for line in my_reader:
if last_line != []:
writer.writerow(last_line + line)
last_line = []
elif line[-1] == "foo":
last_line = line
else:
writer.writerow(line)
if last_line != []: # when the last line also contain "foo"
writer.writerow(last_line)
Tested on a test.csv file:
this,"is,a ",book
this,is,foo
oh,my
this,foo
And gained a new.csv file:
this,"is,a ",book
this,is,foo,oh,my
this,foo
Let's say I have this textfile: date.txt. month|day|year
January|20|2014
February|10|
March|5|2013
I want to put 2012 after February|10|. how can i do that?
You need to read the file into memory, modify the desired line and write back the file.
temp = open('temp', 'wb')
with open('date.txt', 'r') as f:
for line in f:
if line.startswith('February'):
line = line.strip() + '2012\n'
temp.write(line)
temp.close()
shutils.move('temp', 'data.txt')
If you don't want to use a temporary file:
with open('date.txt', 'r+') as f: #r+ does the work of rw
lines = f.readlines()
for i, line in enumerate(lines):
if line.startswith('February'):
lines[i] = lines[i].strip() + '2012\n'
f.seek(0)
for line in lines:
f.write(line)
You can use the csv module, for example:
import csv
data = [
"January|20|2014",
"February|10|",
"March|5|2013"
]
reader = csv.reader(data, delimiter="|")
for line in reader:
line = [i if i != "" else "2012" for i in line]
print(line)
Please note: csv.reader() take as argument any iterable object. So, you can easily pass it a file object