Skip to main content

How to Writing into CSV File with Python Programming

how to write into a CSV file with a Python Programming?



So, Here I have given Python Code and CSV file which I was used in Tutorial.
Python Code:
import csv
with open("data.txt", "r") as dataFile:
   dataFileReader = csv.reader(dataFile)
   dataList = []
   for row in dataFileReader:
     if len (row) != 0:
       dataList = dataList + [row]
dataFile.close()
print(dataList)
name = input("Enter name: ")
secondName = input("Enter secondName: ")
value1 = input("value1: ")
value2 = input("value2: ")
value3 = input("value3: ")
with open("data.txt", "a") as dataFile:
  dataFileWriter = csv.writer(dataFile)
  dataFileWriter.writerow([name, secondName, value1, value2, value3])
dataFile.close()
CSV File :
ABC, XYZ, 1, 2, 3
PQR, WXY, 4, 5, 6
DEF, JKL, 7, 8, 9
This Python code is made for a view, imports, and write the comma separated files in python app. Also with this code user can read that file in the app also They can write in that file.
import csv
with open("data.txt", "r") as dataFile:
 dataFileReader = csv.reader(dataFile)
 dataList = []
 for row in dataFileReader:
 if len (row) != 0:
 dataList = dataList + [row]
dataFile.close()
print(dataList)
Above code is used for a view and import the CSV file. and for more detail of the code check 'Reading of CSV file in python programming'.
name = input("Enter name: ")
secondName = input("Enter secondName: ")
value1 = input("value1: ")
value2 = input("value2: ")
value3 = input("value3: ")
Now, form here writing part begins. Here above we can see for writing into CSV we have to take first input from users with an 'input' function. so I took five inputs for respectively name, secondName, value1, value2, value3.
Those variable's values depend on CSV file's data. Here in above CSV file, we have five variable where two of them have a string value and three of them have a numerical value.
with open("data.txt", "w") as dataFile:
 dataFileWriter = csv.writer(dataFile)
 dataFileWriter.writerow([name, secondName, value1, value2, value3])
Now, we have to reopen the CSV file because we did close once after that reading done. with 'with open' we can open that file and here all things are change is just defining symbol of the process. which has I put 'w' because we have to write into a file so for that this symbol is using. And the second line also has only one difference that we have to use 'csv.writer' instead of 'csv.reader'.
 As you seen name difference so might you get the mean of that name, but still the 'csv.writer' is used to make write operation on CSV file. Which is stored in a 'dataFile' variable. Now the last one line is used for an import the data by passing the array of variables in a same manner of the file. And this array will be written because of 'writerow' function which makes write into file row by row.  
And one important thing is, When we use 'w' symbol then it'll be altering the file data with the importing data. that means after this operation file's data will erase and imported data take place in the file. But if we use 'a' instead of 'w' so it'll add the new data after the old one.
with open("data.txt", "a") as dataFile:
Above one is the syntax for how you can 'a' symbol for an adding data into CSV file.
You can find this video tutorial of this as well by following:
Write into CSV file in Python Programming 

If you have any questions and queries, comment us below.

Comments

Popular posts from this blog

how to pass one variable to other page or same page | JS | HTML

How to pass one variable to another page or same page?So, Here I have given two file's code where one is data sending file. And another one is data receiving file if we transfer to another page. index.html<form action="#tp" method="get"> <input type="text" name="serialNumber" Value="rajnish" /> <input type="submit" value="Submit" /> </form> <div id="tp"> <div id="data"> </div> </div> <script> function processForm() { var parameters = location.search.substring(1).split("&"); var temp = parameters[0].split("="); l = unscape(temp[1]); document.getElementById("data").innerHTML = l; } processForm(); </script>Here above code will be used only when you want to pass a variable on one page only. <form action="#tp" method="get"> <input type="t…

Reading of CSV File in Python Programming

How to read CSV file into Python Programming?
So, Here I have given Python Code and CSV file which I was used in Tutorial. Python Code:import csv with open("data.txt", "r") as dataFile: dataFileReader = csv.reader(dataFile) dataList = [] for row in dataFileReader: if len (row) != 0: dataList = dataList + [row] dataFile.close()print(dataList)CSV File :ABC, XYZ, 1, 2, 3 PQR, WXY, 4, 5, 6 DEF, JKL, 7, 8, 9This Python code is made for a view and imports the comma separated files in python app. Also with this code user can only read this code in the app but They can't write in the file. import csvNow, Let's start with the first line of the program. So, in this line, i had imported the CSV library. and because of that, we can use functions like CSV file open, close, edit, read, write and etc. with open("data.txt", "r") as dataFile:This line has the function called 'open' which will be written 'with'. So, In this function, …

How to make attractive animation with css

How to make an attractive animation with CSS?

So, here I have attached the code which we'll use in this tutorial. CSS Code:   @keyframes shake {   10%, 90% {     transform: translate3d(-1px, 0, 0);   }
  20%, 80% {     transform: translate3d(2px, 0, 0);   }
  30%, 50%, 70% {     transform: translate3d(-4px, 0, 0);   }
  40%, 60% {     transform: translate3d(4px, 0, 0);   }   }
  @keyframes bounce {   10%, 90% {     transform: translate3d(0, 5px, 0);   }
  20%, 80% {     transform: translate3d(0, 3px, 0);   }
  30%, 50%, 70% {     transform: translate3d(0, 7px, 0);   }
  40%, 60% {     transform: translate3d(0, 2px, 0);   }   }
  @keyframes circleanimation {     0%{       transform: scale(0.5);       opacity: 0;     }     60%{       transform: scale(1.4);       opacity: 1;     }     100%{       transform: scale(1);     }   }
  @keyframes arrow {     0% { border-left: 4.5vw solid #0C6095;}     50% {border-left: 5.5vw solid #0C6095;}     100% {border-left: 4.5vw solid #0C6095;}   }
  @keyframes arrow2 {     0% { border-left: 4.5vw …