Python: write multiple lists to multiple rows in csv - python

At the moment, I know how to write a list to one row in csv, but when there're multiple rows, they are still written in one row. What I would like to do is write the first list in the first row of csv, and the second list to the second row.
Code:
for i in range(10):
final=[i*1, i*2, i*3]
with open ('0514test.csv', 'a') as file:
file.write(','.join(str(i) for i in kk))

You may want to add linebreak to every ending of row. You can do so by typing for example:
file.write('\n')

The csv module from standard library provides objects to read and write CSV files. In your case, you could do:
import csv
for i in range(10):
final = [i*1, i*2, i*3]
with open("0514test.csv", "a", newline="") as file:
writer = csv.writer(file)
writer.writerow(final)
Using this module is often safer in real life situations because it takes care of all the CSV machinery (adding delimiters like " or ', managing the cases in which your separator is also present in your data etc.)

Related

how to write comma separated list items to csv in a single column in python

I have a list(fulllist) of 292 items and converted to data frame. Then tried writing it to csv in python.
import pandas as pd
my_df = pd.DataFrame(fulllist)
my_df.to_csv('Desktop/pgm/111.csv', index=False,sep=',')
But the some comma separated values fills each columns of csv. I am trying to make that values in single column.
Portion of output is shown below.
I have tried with writerows but wont work.
import csv
with open('Desktop/pgm/111.csv', "wb") as f:
writer = csv.writer(fulllist)
writer.writerows(fulllist)
Also tried with "".join at each time, when the length of list is higher than 1. It also not giving the result. How to make the proper csv so that each fields fill each columns?
My expected output csv is
Please keep in mind that .csv files are in fact plain text files and understanding of .csv by given software depends on implementation, for example some might allow newline character as part of field, when it is between " and ", while other treat every newline character as next row.
Do you have to use .csv format? If not consider other possibilities:
DSV https://en.wikipedia.org/wiki/Delimiter-separated_values is similiar to csv, but you can use for example ; instead of ,, which should help if you do not have ; in your data
openpyxl allows writing and reading of .xlsx files.

How do I use the csv module to add a column?

I've got a csv file with a matrix of data. When the initial csv file is opened in Notepad it looks like this:
"AAA,15.0"
"BBB,45.0"
"CCC,60.0"
I then want to process this data, adding another column to get something formatted as follows:
"AAA,15.0,50.0"
"BBB,45.0,30.0"
"CCC,60.0,20.0"
So.......
I open the original file into Python using:
with open((FilePath"/XXX.csv"), 'rt') as csvfile:
NewData = list(csv.reader(csvfile, delimiter=';'))
print(NewData)
The first time I do this the code produces a list of strings (which I'm actually happy about - I want this format)...
['AAA,15.0,50.0', 'BBB,45.0,30.0', 'CCC,60.0,20.0']
But then next time I try to add a column I end up with....
[['AAA,15.0,50.0'], ['BBB,45.0,30.0'], ['CCC,60.0,20.0']]
So, each time my code runs it adds an additional layer of 'listing'.
What do I need to do to keep the initial formatting of a list of strings? I imagine it's because I'm opening the file with the list() command. What should I be using?
Further details as requested.........
Distilling this further...My code is...
import csv
FilePathSB="C:/Users/"
with open((FilePathSB+"/Master.csv"), 'rt') as csvfile:
xMatrix = list(csv.reader(csvfile, delimiter=';'))
####Do something to the data like add another column of numbers
#SaveAs same file
with open(FilePathSB+"/Master.csv", "w") as output:
writer=csv.writer(output,lineterminator='\n')
for val in xMatrix:
writer.writerow([val])
Note that there is some data manipulation that occurs while the file is open but this doesn't impact the problem I have so I've left the code out.
Opening the file and then saving it is adding a layer of 'listing' each time the code runs. I'd like the formatting to remain unchanged (ie so despite being opened and then resaved I'd like the data format to be the same as the initial matrix shown below).
So the first time the code runs it opens the initial csv data of:
"AAA,24:17"
"BBB,21:18"
"CCC,16:40"
and changes the format to saves it as:
"['AAA,24:17']"
"['BBB,21:18']"
"['CCC,16:40']"
If I run the code again it takes this data and changes it to:
"[""['AAA,24:17']""]"
"[""['BBB,21:18']""]"
"[""['CCC,16:40']""]"
And if I run it again I end up with:
"['[""[\'AAA,24:17\']""]']"
"['[""[\'BBB,21:18\']""]']"
"['[""[\'CCC,16:40\']""]']"
The csv Reader is meant to parse your file row by row, and return a list for each one.
If we had a file like:
header1|header2
1| A
2| B
when we parsed this csv file using the "|" character as a delimiter, we'd get:
[['header1', 'header2'], ['1', 'A'], ['2', 'B']]
This is exactly what we're supposed to expect in this case. However, if we parsed it with some other character as the delimiter, we'd still get:
[['header1|header2'], ['1| A'], ['2| B']]
This is what you're doing, because your csv reader is primed to expect a delimiter of ";", while your actual csv has (apparently) a delimiter of ",".
After reading your csv in using reader, you'll have a list of lists, where each inner list represents a row. Think of it as looking like this:
[
row1,
row2,
row3
]
where each row looks like:
[cell1, cell2, cell3]
If you want to add a new column to each row, you'll have to iterate over all the rows:
for current_row in rows:
# use current row here
and use the .append() method of a list to add the new column.
current_row.append('new_value')
Finally, you can use csv.writer to write your rows to another file. See csv.writerows

how to write a string to csv?

help please write data horizontally in csv-file.
the following code writes this:
import csv
with open('some.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows('jgjh')
but I need so
The csv module deals in sequences; use writer.writerow() (singular) and give it a list of one column:
writer.writerow(['jgjh'])
The .writerow() method will take each element in the sequence you give it and make it one column. Best to give it a list of columns, and ['jgjh'] makes one such column.
.writerows() (plural) expects a sequence of such rows; for your example you'd have to wrap the one row into another list to make it a series of rows, so writer.writerows([['jgjh']]) would also achieve what you want.

python:compare two large csv files by two reference columns and update another column

I have quite large csv file, about 400000 lines like:
54.10,14.20,34.11
52.10,22.20,22.11
49.20,17.30,29.11
48.40,22.50,58.11
51.30,19.40,13.11
and the second one about 250000 lines with updated data for the third column - the first and the second column are reference for update:
52.10,22.20,22.15
49.20,17.30,29.15
48.40,22.50,58.15
I would like to build third file like:
54.10,14.20,34.11
52.10,22.20,22.15
49.20,17.30,29.15
48.40,22.50,58.15
51.30,19.40,13.11
It has to contain all data from the first file except these lines where value of third column is taken from the second file.
Suggest you look at Pandas merge functions. You should be able to do what you want,, It will also handle the data reading from CSV (create a dataframe that you will merge)
A stdlib solution with just the csv module; the second file is read into memory (into a dictionary):
import csv
with open('file2.csv', 'rb') as updates_fh:
updates = {tuple(r[:2]): r for r in csv.reader(updates_fh)}
with open('file1.csv', 'rb') as infh, open('output.csv', 'wb') as outfh:
writer = csv.writer(outfh)
writer.writerows((updates.get(tuple(r[:2]), r) for r in csv.reader(infh)))
The first with statement opens the second file and builds a dictionary keyed on the first two columns. It is assumed that these are unique in the file.
The second block then opens the first file for reading, the output file for writing, and writes each row from the inputfile to the output file, replacing any row present in the updates dictionary with the updated version.

How to export data (which is as result of Python program) from command line?

I am working on a Python program, and I have results on the command line.
Now I need to do analysis on the results, so I need all results as exported in any format like either SQL, or Excel or CSV format.
Can some tell me how can i do that ?
import csv
x1=1 x2=2
while True:
show = [ dict(x1=x1+1 , x2=x2+2)]
print('Received', show )
with open('large1.csv','w') as f1:
writer=csv.writer(f1, delimiter=' ',lineterminator='\n\n',)
writer.writerow(show)
x1=x1+1
x2=x2+1
Here this is infinite loop and I want to have a csv file containing 2 column of x1 and x2. and with regularly updated all values of x1 and x2 row wise (1 row for 1 iteration)
But by this code I'm getting a csv file which is named as 'large1.csv' and containing only one row (last updated values of x1 and x2).
So how can I get my all values of x1 and x2 as row was in python.
Just use the csv format it can easily imported into Excel and
the python standard library supports csv out of the box. #See python-csv
One way to handle this is to use the CSV module, and specifically a Writer object to write the output to a CSV file (perhaps even instead of writing to stdout). The documentation has several examples, including this one:
import csv
with open('some.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(someiterable)
You should then be able to import the CSV file easily in Excel if that is what you want.
Have a look at the open() documentation.
Mode w will truncate the file, which means it will replace the contents. Since you call that every loop iteration, you are continuously deleting the file and replacing it with a new one. Mode a appends to the file and is maybe what you want. You also might consider opening the file outside of the loop, in that case w might be the correct mode.

Categories

Resources