how to write a single row cell by cell and fill it in csv file

2024/7/8 7:43:53

I have a CSV file that only has column headers:

 cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3

I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optimized way to do that in Python and Pandas?

After I fill the row the csv file should look like:

 cat mycsv.csvcol_1 , col_2 , col_3 , col_30@@@None@@@None@@@None

How to insert index and separator is @@@ instead of , ? I am getting an error as:

tmp.to_csv(csvFile,sep='@@@',index=True) , line 1381, in to_csv
formatter.save()3.6.1/linux_x86_64/lib/python3.6/site-packages/pandas/formats/format.py", line 1473, in save
self.writer = csv.writer(f, **writer_kwargs)TypeError: "delimiter" must be a 1-character string
Exception ignored in: <module 'threading' from Traceback (most recent call last):
File python-3.6.1/linux_x86_64/lib/python3.6/threading.py", line 1015, in 
_delete
del _active[get_ident()] 
KeyError: 140459817715712
Answer

Does it a @ or , ? If you using a formal csv file, the separator of the header should be the same as the contents.

If you mean ,, you could use something like that.

import pandas as pd
# just for abrevation
tmp = pd.read_csv("mycsv.csv",sep=',')
tmp.loc[0,:] = "None"
tmp.to_csv("mycsv.csv",sep=',',index=False)

If you mean @, I suggest that you should not use pandas. Just using the simply IO ways.

tmp = open("mycsv.csv","r").read()
tmp = tmp + "\n" + "@@@".join(["None"] * len(tmp.split(',')))
with open("mycsv.csv","w") as f1:f1.write(tmp)
https://en.xdnf.cn/q/119569.html

Related Q&A

Greedy String Tiling in Python

I am trying to learn greedy string tiling in algorithmI have two lists as follows:a=[a,b,c,d,e,f] b=[d,e,a,b,c,f]i would like to retrieve c=[a,b,c,d,e]Another example would be a = [1,2,3,4,5,6,7,8,9,1,…

Python - efficient way to create 20 variables?

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the var…

Whatsapp asking for updating chrome version

I am trying to open whatsapp with selenium and python, it was working fine until today. In headless or non, whatsapp is now asking to update chrome, when I try to do so, Chrome throws this error: An er…

how to find the longest N words from a list, using python?

I am now studying Python, and I am trying to solve the following exercise:Assuming there is a list of words in a text file, My goal is to print the longest N words in this list.Where there are several …

([False, True] and [True, True]) evaluates to [True, True]

I have observed the following behavior in python 3: >>> ([False, True] and [True, True]) [True, True]>>> ([False, True] or [True, True]) [False, True]I was expecting exactly the oppos…

Uploading an image to Flask server

I am struggling bit with Flask and uploading a file, here is my Flask code so far:@app.route(/api/user/update/, methods=[PUT]) @auth.login_required def update_user():# check if the post request has the…

Enemy Projectiles Arent Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles arent showing on my screen VIDEO. He isnt attacking at all, I dont know why. I am in my main loop I draw t…

Python+Selenium. Cant locate element

Ive implemented the script using Python and selenium to click on the ads. But now this script is not working.Unable to find element on the page.Please help me to correct the script. Thank you!from sele…

AttributeError: NoneType object has no attribute channels [duplicate]

This question already has answers here:Why do I get AttributeError: NoneType object has no attribute something?(11 answers)Closed 5 years ago.Hi Im having an issue with a module for my Discord bot. Im…

How to get a specific value from a html header

Im using selenium to get request headers from a web page, the problem is that it prints out all request headers sent and i want to get only one value from one of them. I dont know how to do it and i ha…