How to write nth value of list into csv file

2024/10/8 6:24:06

i have the following list :

sec_min=['37', '01', '37', '02', '37', '03', '37', '04', '37', '05',....]

i want to store this list into CVS file in following format:

37,01
37,02
37,03
37,04 ... and so on

this is what i coded:

with open('datas.csv','w')as f:data=csv.writer(f)for value in sec_min:data.writerow(sec_min[:2])

(i.e. every two elements of list in new row)

Answer

compose a "matrix" of row length = 2, and write all the rows at once in a generator comprehension

with open('datas.csv','wb') as f:  # python 2
with open('datas.csv','w',newline='') as f:  # python 3data=csv.writer(f)data.writerows(sec_min[l:l+2] for l in range(0,len(sec_min),2))

note the different way of opening a file for csv module whether you're running python 2 or python 3.

Your original method results in blank lines every other line because of extra \r chars.

https://en.xdnf.cn/q/118730.html

Related Q&A

Read R function output as columns

Im trying to come up with a way to solve this question I asked yesterday:rpy2 fails to import rgl R packageMy goal is to check if certain packages are installed inside R from within python.Following th…

How does .split() work? - Python

In the following examples, I am splitting an empty string by a space. However, in the first example I explicitly used a space and in the second example, I didnt. My understanding was that .split() and …

How to get email.Header.decode_header to work with non-ASCII characters?

Im borrowing the following code to parse email headers, and additionally to add a header further down the line. Admittedly, I dont fully understand the reason for all the scaffolding around what should…

Elif syntax error in Python

This is my code for a if/elif/else conditional for a text-based adventure game Im working on in Python. The goal of this section is to give the player options on what to do, but it says there is someth…

Convert date from dd-mm-yy to dd-mm-yyyy using python [duplicate]

This question already has answers here:How to parse string dates with 2-digit year?(6 answers)Closed 7 years ago.I have a date input date_dob which is 20-Apr-53 I tried converting this to format yyyy…

sklearn pipeline transform ValueError that Expected Value is not equal to Trained Value

Can you please help me to with the following function where I got the error of ValueError: Column ordering must be equal for fit and for transform when using the remainder keyword(The function is calle…

How to show Chinese characters in Matplotlib graphs?

I want to make a graph based on a data frame that has a column with Chinese characters. But the characters wont show on the graph, and I received this error. C:\Users\march\anaconda3\lib\site-packages\…

nginx flask aws 502 Bad Gateway

My server is running great yesterday but now it returned a 502 error, how could this happen?In my access.log shows:[24/Aug/2016:07:40:29 +0000] "GET /ad/image/414 HTTP/1.1" 502 583 "-&q…

Let discord bot interact with other bots

I have a Python script for a Discord bot and I want it to send a message to another Bot and select the prompt option and then type in a message but I cant get the interaction done. It just sends the me…

Image has 3 channels but its in a grayscale color. If I change it to 1 channel, it goes into RGB

I started doing some image-processing in python and Ive stumbled upon an issue which is kind of confusing from a beginners perspective. I have a dataset of 1131 np arrays (images) of MRI on knee. The s…