How to write to a CSV line by line?
General way:
##text=List of strings to be written to file
with open('csvfile.csv','wb') as file:
for line in text:
file.write(line)
file.write('\n')
OR
Using CSV writer :
import csv
with open(<path to output_csv>, "wb") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
writer.writerow(line)
OR
Simplest way:
f = open('csvfile.csv','w')
f.write('hi there\n') #Give your csv text here.
## Python will convert \n to os.linesep
f.close()
How to write a csv file line by line?
In this particular case (in other words, not in the most general sense), specifying the first element of List1
as the fillvalue
argument when calling itertools.zip_longest()
looks like it would make it work:
import csv, itertools
List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish', 'takeout,fancy presentation, piled']
with open("Output.csv", "w", newline="") as Output_File:
Output_File.write("List1,List2,List3\n")
writer = csv.writer(Output_File)
rows = itertools.zip_longest(List1, List2, List3, fillvalue=List1[0])
writer.writerows(rows)
Contents of output.csv
file afterward:
List1,List2,List3
DealerName,Person1,"crisp nori, hot rice, and cold fish"
DealerName,Person2,"takeout,fancy presentation, piled"
Writing to a CSV file with only one header line
There are two issues with your code, the first is data
is an list yet you're enclosing it in another list, i.e. [header]
is the same as [['cost', 'tax', 'percentage_of_pay']]
which is an list of lists.
Second you would normally write the header first then write the data in a loop one per data row
You probably want something like:
with open('Sales-and-cost.csv', 'w') as f:
writer=csv.writer(f, delimiter='\t', lineterminator='\n')
writer.writerow(header)
for row in rows:
writer.writerow(row)
Where rows is a list of lists containing the output data, i.e.
rows = [[price_1, taxes_1, pay_percentage_1],[price_2, taxes_2, pay_percentage_2],[price_3, taxes_3, pay_percentage_3]]
How to write a pandas dataframe to CSV file line by line, one line at a time?
If you really want to print line by line. (You should not).
for i in range(len(df)):
df.loc[[i]].to_csv(output_csv_file,
index=False,
header=False,
mode='a')
How to read a CSV file line by line and write to another CSV file. How to skip the first 4 rows while writing to another file using C#?
To Skip the first 4 lines of the input file you need to read a line on every iteration of the while
loop
using (var writer = new StreamWriter(File.OpenWrite("D:\\work\\POCs\\POC_RESOURCES\\Pricing_Files\\index-edited-ap-east-1.csv")))
using (var reader = new StreamReader(File.OpenRead("D:\\work\\POCs\\SPOT_POC_RESOURCES\\Pricing_Files\\index-ap-east-1.csv")))
{
var index = 0;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (index > 4)
{
Console.WriteLine(index);
writer.WriteLine(line);
}
index++;
}
}
Python CSV, How to append data at the end of a row whilst reading it line by line (row by row)?
Do backup your file before trying just in case.
You can write a new temporary file and move that into the place over the old file you read from.
from tempfile import NamedTemporaryFile
import shutil
import csv
filename = 'candidates.csv'
tempfile = NamedTemporaryFile('w', delete=False)
with open(filename, 'r', newline='') as csvFile, tempfile:
writer = csv.writer(tempfile)
for line in csvFile:
csv_row = line.strip().split(',')
csv_row.append(check_update(csv_row[7])) # this will add the data to the end of the list.
writer.writerow(csv_row)
shutil.move(tempfile.name, filename)
R write list to CSV line by line
Rather than write serially, you can convert to a data.frame and just write all at once:
Generate fake data that looks similar to what you posted:
fakeVec <- function(dummy) t(setNames(rnorm(3), letters[1:3]))
my_list <- lapply(0:4, fakeVec)
names(my_list) <- 6000:6004
Here's the fake data:
$`6000`
a b c
[1,] -0.2444195 -0.2189598 -1.442364
$`6001`
a b c
[1,] 0.2742636 1.068294 -0.8335477
$`6002`
a b c
[1,] -1.13298 1.927268 -2.123603
$`6003`
a b c
[1,] 0.8260184 1.003259 -0.003590849
$`6004`
a b c
[1,] -0.2025963 0.1192242 -1.121807
Then convert format:
# crush to flat matrix
my_mat <- do.call(rbind, my_list)
# add in list names as new column
my_df <- data.frame(id = names(my_list), my_mat)
Now you have a data.frame like this:
id a b c
1 6000 -0.2444195 -0.2189598 -1.442364429
2 6001 0.2742636 1.0682937 -0.833547659
3 6002 -1.1329796 1.9272681 -2.123603334
4 6003 0.8260184 1.0032591 -0.003590849
5 6004 -0.2025963 0.1192242 -1.121807439
Which you can just write straight to a file:
write.csv(my_df, 'my_file.csv', row.names=F)
How do I write to a CSV in Python, and have the new data below the last line?
The clue is you say you want to leave your CSV in a sorted state. To do that will involve first reading the whole file in. Adding your new information, sorting it based on the surname (and possibly then on the firstname). Finally writing the whole thing back.
I am assuming your file has a header. In which case you need to take care not to include that as part of the sort.
For example:
import csv
def append_list_as_row(file_name, row):
filename = "MusicDatacopy.csv"
# Read the existing file in
with open(filename, newline='') as f_input:
csv_reader = csv.reader(f_input)
header = next(csv_reader)
data = list(csv_reader)
data.append(row) # add the new row to the end
data.sort(key=lambda x: (x[5], x[4])) # sort the data based on s_name
# Write the whole file back
with open(filename, 'w', newline='') as f_output:
csv_writer = csv.writer(f_output)
csv_writer.writerow(header)
csv_writer.writerows(data)
artist = input("Enter Artist name: ").title()
album = input("Enter Album name: ").title()
month = input("Enter month bought: ").title()
owner = input("Enter Name of Owner: ").title()
f_name = input("Enter artist f_name: ").title()
s_name = input("Enter artist s_name: ").title()
genre = input("Enter genre: ").title()
row_contents = [artist, album, month, owner, f_name, s_name, genre]
append_list_as_row('MusicDatacopy.csv', row_contents)
Related Topics
How to Log While Using Multiprocessing in Python
In Python, How to Convert a 'Datetime' Object to Seconds
Running a Linux Command from Python
Run a Linux System Command as a Superuser, Using a Python Script
Why Is a List Comprehension So Much Faster Than Appending to a List
Why Isn't My Pandas 'Apply' Function Referencing Multiple Columns Working
How to Remove Non-Ascii Characters But Leave Periods and Spaces
How to Change Backends in Matplotlib/Python
What Can You Use Generator Functions For
How to Specify the Function Type in My Type Hints
Python Ta-Lib Install Error, How Solve It
Importerror: Libcblas.So.3: Cannot Open Shared Object File: No Such File or Directory
How Is the 'Is' Keyword Implemented in Python
Access an Arbitrary Element in a Dictionary in Python