This question already has answers here:
How to add a string to each line in a file?
(3 answers)
Closed 9 months ago.
I have already an existing CSV file that I am accessing and I want to append the data to the first row, but it writes data at the end of the file.
What I am getting:
But I want the data to append like this:
Code I have done so far:
import CSV
with open('explanation.csv' , 'a', newline="") as file:
myFile = csv.writer(file)
myFile.writerow(["1"])
What you're actually wanting to do is replace data in an existing CSV file with new values, however in order to update a CSV file you must rewrite the whole thing.
One way to do that is by reading the whole thing into memory, updating the data, and then use it to overwrite the existing file. Alternatively you could process the file a row-at-a-time and store the results in a temporary file, then replace the original with the temporary file when finished updating them all.
The code to do the latter is shown below:
import csv
import os
from pathlib import Path
from tempfile import NamedTemporaryFile
filepath = Path('explanation.csv') # CSV file to update.
with open(filepath, 'r', newline='') as csv_file, \
NamedTemporaryFile('w', newline='', dir=filepath.parent, delete=False) as tmp_file:
reader = csv.reader(csv_file)
writer = csv.writer(tmp_file)
# Replace value in the first column of the first 5 rows.
for data_value in range(1, 6):
row = next(reader)
row[0] = data_value
writer.writerow(row)
writer.writerows(reader) # Copy remaining rows of original file.
# Replace original file with updated version.
os.replace(tmp_file.name, filepath)
print('CSV file updated')
You could read in the entire file, append your rows in memory, and then write the entire file:
def append(fname, data):
with open(fname) as f:
reader = csv.reader(f)
data = list(reader) + list(data)
with open(fname, 'w') as f:
writer = csv.writer(f)
writer.writerows(data)
I have a list like the following with the \n separating each new lines
['Data,9,record,timestamp,"896018545",s,position_lat,"504719750",semicircles,position_long,"-998493490",semicircles,distance,"10.87",m,altitude,"285.79999999999995",m,speed,"1.773",m/s,unknown,"3929",,unknown,"1002",,enhanced_altitude,"285.79999999999995",m,enhanced_speed,"1.773",m/s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n', 'Data,9,record,timestamp,"896018560",s,position_lat,"504717676",semicircles,position_long,"-998501870",semicircles,distance,"71.85",m,altitude,"285.0",m,speed,"5.533",m/s,unknown,"3924",,unknown,"1001",,enhanced_altitude,"285.0",m,enhanced_speed,"5.533",m/s,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]
This is hard to read, so I need to extract the following out of this list into a CSV file in python in the below format without using pandas:
timestamp, position_lat,altitude
"896018545","504719750","285.79999999999995"
"896018560","504717676","285.0"
I have the following, but I am confused about how to add the data into the CSV file:
header = ['timestamp','latitude','altitude']
with open('target.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
# write the data
If I'm understanding your question correctly, all you need to do is write additional rows that include your data.
...
...
writer.writerow(["896018545","504719750","285.79999999999995"])
writer.writerow(["896018560","504717676","285.0"])
# alternatively,
data = [["896018545","504719750","285.79999999999995"],
["896018560","504717676","285.0"]]
...
...
for row in data:
writer.writerow(row)
I want to read in a csv file, sort it, then rewrite a new file. Any help?
You should probably take a look at the python documentation of the csv module:
https://docs.python.org/3.6/library/csv.html
You could also use pandas, but that might be overkill if you're new to python.
To give you some initial code to play with:
# file test.csv
2,a,x
0,b,y
1,c,z
Code:
import csv
csv_lines = []
# read csv
with open('test.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
csv_lines.append(row)
# sort by first column
csv_lines_sorted = sorted(csv_lines, key=lambda x: x[0])
# write csv
with open('test_sorted.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for row in csv_lines_sorted:
writer.writerow(row)
I'm developing a script in python that gets data from a log file and I need to save every type of data into a respective column. I'm using regex to obtain the data.
This is a part of my code where I get this result:
#Getting data from log as list using regex
fecha = re.findall('\d{4}\-\d{2}\-\d{2}', str(listaValores))
hora = re.findall('\d{2}\:\d{2}\:\d{2}', str(listaValores))
#List of lists about data obtained
valoresFinales = [fecha, hora]
#Putting into .csv
with open("resultado.csv", "w") as f:
wr = csv.writer(f, delimiter=';')
wr.writerows(valoresFinales)
What I want
You give the writerows function a list of two elements, so you get two rows of data in the end.
Instead you want to give it something like zip(fecha, hora):
with open("resultado.csv", "w") as f:
wr = csv.writer(f, delimiter=';')
wr.writerows(zip(*valoresFinales))
Hi I'm writing a simple script to copy a set of rows from a csv file and paste them for N number of times in other file.
I'm not able to write the result into other file.
Please find the code below:
import csv
for i in range(2):
with open('C:\\Python\\CopyPaste\\result2.csv', 'r') as fp:
data = fp.readlines()
fp.close()
with open('C:\\Python\\CopyPaste\\mydata.csv', 'w') as mycsvfile:
thedatawriter = csv.writer(mycsvfile)
for row in data:
thedatawriter.writerow(row)
Assuming that the format of the input and output CSV files is the same, just read the input file into a string and then write it to an output file N times:
N = 3
with open('C:\\Python\\CopyPaste\\result2.csv', 'r') as infile,\
open('C:\\Python\\CopyPaste\\mydata.csv', 'w') as outfile:
data = fp.read() # read entire contents of input file into data
for i in range(N):
outfile.write(data)
The above answers the question literally, however, it will replicate the header row N times, probably not what you want. You can do this instead:
import csv
N = 3
with open('C:\\Python\\CopyPaste\\result2.csv', 'r') as infile,\
open('C:\\Python\\CopyPaste\\mydata.csv', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
writer.writerow(next(reader)) # reads header line and writes it to output file
data = [row for row in reader] # reads the rest of the input file
for i in range(N):
writer.writerows(data)
This code reads the first row from the input file as the header, and writes it once to the output CSV file. Then the remaining rows are read from the input file into the data list, and replicated N times in the output file.
I guess your question is : read a .csv file and then write the data to another .csv file for N times?
If my recognition is right, my suggestion would be using pandas library, that's very convenient.
Something like:
import pandas as pd
df = pd.read_csv('origin.csv')
df.to_csv('output.csv')