I have a list of numbers that I want to put in a single column in a .csv file. The code below writes the values across a single row. How can I change the code so that Python writes the each value on a separate row? Thanks.
with open('returns.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(daily_returns)
With Python3, open the file in w mode:
with open('returns.csv', 'w') as f:
writer = csv.writer(f)
for val in daily_returns:
writer.writerow([val])
With Python2.6+, open the file in wb mode:
with open('returns.csv', 'wb') as f:
writer = csv.writer(f)
for val in daily_returns:
writer.writerow([val])
Alternate solution: Assuming daily_returns is the name of the list you wish to write as a column in a CSV file, the following code should work:
with open('return.csv','w') as f:
writer = csv.writer(f)
writer.writerows(zip(daily_returns))
Just for the record:
I'm using Python 3.2 and I could only get the following to work
with open('returns','w')as f:
writer=csv.writer(f,lineterminator='\n')
for val in returns:
writer.writerow([val])
For writing a single column, I would recommend avoiding the csv commands and just using plain python with the str.join() method:
with open('returns.csv', 'wb') as f:
f.write("\n".join(daily_returns))
Related
I have a list of numbers that I want to put in a single column in a .csv file. The code below writes the values across a single row. How can I change the code so that Python writes the each value on a separate row? Thanks.
with open('returns.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(daily_returns)
With Python3, open the file in w mode:
with open('returns.csv', 'w') as f:
writer = csv.writer(f)
for val in daily_returns:
writer.writerow([val])
With Python2.6+, open the file in wb mode:
with open('returns.csv', 'wb') as f:
writer = csv.writer(f)
for val in daily_returns:
writer.writerow([val])
Alternate solution: Assuming daily_returns is the name of the list you wish to write as a column in a CSV file, the following code should work:
with open('return.csv','w') as f:
writer = csv.writer(f)
writer.writerows(zip(daily_returns))
Just for the record:
I'm using Python 3.2 and I could only get the following to work
with open('returns','w')as f:
writer=csv.writer(f,lineterminator='\n')
for val in returns:
writer.writerow([val])
For writing a single column, I would recommend avoiding the csv commands and just using plain python with the str.join() method:
with open('returns.csv', 'wb') as f:
f.write("\n".join(daily_returns))
Why is my CSV write function not working? This seems to be a very simple code but the CSV writerow is asking for an iterable. I just need to write the 1,2,3 in a column.
import csv
data = [1,2,3]
output = 'output.csv'
with open(output,'w') as f:
writer = csv.writer(f)
for item in data:
writer.writerow(item)
You are passing an integer but a list is expected.
import csv
data = [[1],[2],[3]]
output = 'output.csv'
with open(output,'w') as f:
writer = csv.writer(f)
for item in data:
writer.writerow(item)
We need to provide a list to the CSV writer writerow function, in which there are values for 1 complete row.
e.g
import csv
data = [1,2,3]
output = 'output.csv'
with open(output,'w') as f:
writer = csv.writer(f)
writer.writerow(data) # as data includes only 1 row. For multiple rows write every row with a loop
Sorry for asking i have searched a lot but cant find what i need.
I need this list to be written to a csv file on one row(1) and each element from A to E
coluna_socio = ['bilhete', 'tipo', 'nome', 'idade', 'escalao']
outfile = open('socios_adeptos.csv', 'w', newline='')
writer = csv.writer(outfile)
for i in range(len(coluna_socio)):
writer.writerow([coluna_socio[i]])
Ive tried almost everything and it always write on on column or just in on cell(A1)
Thanks.
You can use the string join method to insert a comma between all the elements of your list. Then write the resulting string to file. The csv module isn't necessary in this case.
For example ...
with open('socios_adeptos.csv', 'w') as out:
out.write(','.join(coluna_socio))
You should call the csv.writer.writerow method with the list directly:
with open('socios_adeptos.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerow(coluna_socio)
I could write it in one row with your code, as follows:
coluna_socio = ['bilhete', 'tipo', 'nome', 'idade', 'escalao']
outfile = open('socios_adeptos.csv', 'w', newline='')
writer = csv.writer(outfile)
writer.writerow(coluna_socio)
outfile.close()
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 would like to create a subset of a large CSV file using the rows that have the 4th column ass "DOT" and output to a new file.
This is the code I currently have:
import csv
outfile = open('DOT.csv','w')
with open('Service_Requests_2015_-_Present.csv', newline='', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
if row[3] == "DOT":
outfile.write(row)
outfile.close()
The error is:
outfile.write(row)
TypeError: must be str, not list
How can I manipulate row so that I will be able to just straight up do write(row), if not, what is the easiest way?
You can combine your two open statements, as the with statement accepts multiple arguments, like this:
import csv
infile = 'Service_Requests_2015_-_Present.csv'
outfile = 'DOT.csv'
with open(infile, encoding='utf-8') as f, open(outfile, 'w') as o:
reader = csv.reader(f)
writer = csv.writer(o, delimiter=',') # adjust as necessary
for row in reader:
if row[3] == "DOT":
writer.writerow(row)
# no need for close statements
print('Done')
Make your outfile a csv.writer and use writerow instead of write.
outcsv = csv.writer(outfile, ...other_options...)
...
outcsv.writerow(row)
That is how I would do it... OR
outfile.write(",".join(row)) # comma delimited here...
In Above code you are trying to write list with file object , we can not write list that give error "TypeError: must be str, not list" you can convert list in string format then you able to write row in file. outfile.write(str(row))
or
import csv
def csv_writer(input_path,out_path):
with open(out_path, 'ab') as outfile:
writer = csv.writer(outfile)
with open(input_path, newline='', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
if row[3] == "DOT":
writer.writerow(row)
outfile.close()
csv_writer(input_path,out_path)
[This code for Python 3 version. In Python 2.7, the open function does not take a newline argument, hence the TypeError.]