1 00:00:00,000 --> 00:00:02,300 [no audio] 2 00:00:02,300 --> 00:00:07,700 Friends here we are going to see how to create a CSV file using python, 3 00:00:08,600 --> 00:00:11,700 right? So, let me write a simple Python script just to create 4 00:00:11,700 --> 00:00:12,800 a CSV file. 5 00:00:13,800 --> 00:00:16,900 So whenever if you want to create a CSV file, first of all, 6 00:00:16,900 --> 00:00:20,700 you need to import 'csv' module into your Python script. 7 00:00:20,900 --> 00:00:27,700 Let me write a script name as 'create_csv_file.py'. So 8 00:00:27,700 --> 00:00:29,400 I want to create a CSV file. 9 00:00:29,400 --> 00:00:32,299 So I need to 'import csv' first, right? 10 00:00:32,700 --> 00:00:36,000 So let me create any CSV file in any location. 11 00:00:36,200 --> 00:00:39,500 Let me create a file in this location. 12 00:00:39,500 --> 00:00:42,200 [no audio] 13 00:00:42,200 --> 00:00:44,900 Right. Yeah. See that. Directly 14 00:00:44,900 --> 00:00:49,600 you can take your required, suppose 'req_file' or required 15 00:00:49,600 --> 00:00:54,500 CSV file, so some 'xyz.csv', but where you want to store this? 16 00:00:54,500 --> 00:00:56,500 [no audio] 17 00:00:56,500 --> 00:00:59,700 That's why it's better to provide complete path for your file. 18 00:01:00,400 --> 00:01:05,500 So suppose I want to create my CSV file somewhere in this path. 19 00:01:06,500 --> 00:01:09,800 So this is the path where I want to store my CSV file, and 20 00:01:09,800 --> 00:01:10,900 then what is the name 21 00:01:10,900 --> 00:01:12,200 you want to create here, 22 00:01:12,500 --> 00:01:13,800 I mean for your CSV file? 23 00:01:13,800 --> 00:01:16,300 Let's say 'demo.csv'. 24 00:01:17,000 --> 00:01:18,000 That's it. 25 00:01:18,800 --> 00:01:23,400 Right. See guys, if you have a CSV file, if you want to read the 26 00:01:23,400 --> 00:01:26,200 content of your CSV file what you are doing? First, you are 27 00:01:26,200 --> 00:01:27,500 opening your file 28 00:01:28,800 --> 00:01:32,600 in a 'read' mode, but here we want to write the content into 29 00:01:32,600 --> 00:01:33,600 your CSV file. 30 00:01:33,700 --> 00:01:36,800 Then what we have to do is you need to open this in 'write' 31 00:01:36,800 --> 00:01:37,900 mode. That's it. 32 00:01:39,500 --> 00:01:41,800 Then finally we have to close it. Between this you need to 33 00:01:41,800 --> 00:01:43,900 write the content into your CSV file. 34 00:01:43,900 --> 00:01:46,400 [no audio] 35 00:01:46,400 --> 00:01:47,600 Right. Fine. 36 00:01:47,900 --> 00:01:50,300 See. Yeah, I will do one thing. 37 00:01:50,300 --> 00:01:55,500 First of all, I am going to read the content, then based on 38 00:01:55,500 --> 00:01:57,500 that I will try to convert that script. 39 00:01:58,500 --> 00:02:01,000 I think you have a file called 'new_info'. 40 00:02:01,000 --> 00:02:04,000 [no audio] 41 00:02:04,000 --> 00:02:06,300 Right. Let me read the file content. 42 00:02:06,800 --> 00:02:11,699 So we are going to create a object called 'reader' object, 43 00:02:11,800 --> 00:02:16,400 so your 'csv_reader', or simply 'content', whatever it maybe. 44 00:02:17,800 --> 00:02:23,900 So simply 'csv.reader'. So you are passing your object. Then 45 00:02:25,200 --> 00:02:33,600 'delimiter=","'. Then 'for each_row', right, in your 46 00:02:33,600 --> 00:02:37,800 'csv_reader', then you are able to print 'each_row'. 47 00:02:37,800 --> 00:02:39,900 [no audio] 48 00:02:39,900 --> 00:02:42,500 Yes, I'm going to get something. 49 00:02:42,500 --> 00:02:43,700 What is that? What happened? 50 00:02:43,700 --> 00:02:49,100 [no audio] 51 00:02:49,100 --> 00:02:50,300 'req_file', yeah. 52 00:02:50,300 --> 00:02:54,600 Sorry, that is a variable, right, I mentioned as a string. 53 00:02:54,700 --> 00:02:56,800 That's not correct. Inside a variable 54 00:02:56,800 --> 00:02:57,800 you have a file name. 55 00:02:57,800 --> 00:02:58,800 Yeah, now it's good. 56 00:03:00,300 --> 00:03:02,800 Right. Yeah, 'delimiter' for this file is '|', right? 57 00:03:02,900 --> 00:03:05,500 Let me take '|' as my 'delimiter'. 58 00:03:05,500 --> 00:03:07,500 [no audio] 59 00:03:07,500 --> 00:03:10,800 So guys if you observe 'each_row' is like one list. 60 00:03:12,200 --> 00:03:13,700 Right. See now 61 00:03:13,700 --> 00:03:16,600 what I am doing is based on these steps, 62 00:03:16,900 --> 00:03:20,100 so reading your CSV file is very easy, the same way if we convert 63 00:03:20,100 --> 00:03:23,800 this into, to write the content to your CSV file then it's very 64 00:03:23,800 --> 00:03:28,600 simple. Let me take now create, even I'm going to comment 65 00:03:28,600 --> 00:03:29,600 this file as well. 66 00:03:29,600 --> 00:03:34,100 [no audio] 67 00:03:34,100 --> 00:03:38,600 Now just compare those steps and tell me, so I want to 68 00:03:38,600 --> 00:03:40,800 create a file called 'demo.csv'. 69 00:03:42,500 --> 00:03:45,300 So previously you opened your file for read, now 70 00:03:45,300 --> 00:03:48,800 I want to open this file to write the content, 71 00:03:48,800 --> 00:03:53,700 so 'req_file', 'w' mode. That's it. 72 00:03:54,700 --> 00:03:58,200 So previously you created object called 'csv_reader' to read 73 00:03:58,200 --> 00:04:00,400 the content from your file object. 74 00:04:00,800 --> 00:04:04,600 But now I want to create a writer. Nothing 75 00:04:04,600 --> 00:04:07,100 is there. Instead of 'reader', right? 76 00:04:07,100 --> 00:04:11,400 See that. I want to create a 'csv_writer'. 77 00:04:11,400 --> 00:04:14,200 You don't need to take this variable exactly. You can take any variable. 78 00:04:14,900 --> 00:04:19,100 Right. So I am taking 'csv.writer' 79 00:04:20,800 --> 00:04:25,700 for your file object, then let me take 'delimiter', as of now 80 00:04:25,700 --> 00:04:29,400 I am going to take it as a ','; ',' or '|', 81 00:04:29,400 --> 00:04:31,900 we will see for different symbols, 82 00:04:31,900 --> 00:04:34,300 I mean different delimiters, right. Fine. 83 00:04:35,700 --> 00:04:37,700 Now finally, you need to close your file. 84 00:04:38,700 --> 00:04:42,000 Now you need to write some content into your CSV file, right? 85 00:04:42,300 --> 00:04:46,100 See, whenever if you're reading the content from your CSV 86 00:04:46,100 --> 00:04:50,100 file what you are getting? You're getting a list. The same way 87 00:04:50,100 --> 00:04:53,400 whenever if you want to store the content into your CSV file, 88 00:04:53,700 --> 00:04:58,000 each line must be like a row, that row must be like a list, 89 00:04:58,000 --> 00:05:00,200 that list consists of your columns. 90 00:05:00,700 --> 00:05:05,400 Nothing is there. Let's say I want to store my header, right? 91 00:05:05,400 --> 00:05:07,700 Let me create a header, suppose, 92 00:05:08,600 --> 00:05:12,600 so 'csv_writer.writerow'. 93 00:05:12,900 --> 00:05:14,800 You have to write your row. 94 00:05:15,600 --> 00:05:18,700 So what is our row? Our row must be a list as of now. 95 00:05:20,400 --> 00:05:23,600 And that list consists of different values. 96 00:05:23,700 --> 00:05:26,300 Each value is like one column in your CSV. 97 00:05:26,300 --> 00:05:27,700 That's it. 'serial_number', 98 00:05:28,100 --> 00:05:31,000 let me take 'name', and let me take 'age'. 99 00:05:31,000 --> 00:05:33,700 [no audio] 100 00:05:33,700 --> 00:05:34,700 That's it. 101 00:05:35,900 --> 00:05:38,700 So as of now what I did? I created only header part. 102 00:05:39,200 --> 00:05:41,100 I created only header part. 103 00:05:41,300 --> 00:05:44,800 Let me save it and run it. It's done. 104 00:05:45,400 --> 00:05:49,200 Let me go to my directory where I'm creating and there is 105 00:05:49,200 --> 00:05:50,200 a file called 'demo', 106 00:05:50,200 --> 00:05:54,700 let me open it. Instead of text file directly 107 00:05:54,700 --> 00:05:57,300 I'm going to open it using your Excel. 108 00:05:58,100 --> 00:05:59,600 Let me open it and see the data. 109 00:06:00,900 --> 00:06:04,000 Yes, you're able to get your data. In your CSV file whatever 110 00:06:04,000 --> 00:06:06,200 you have that you are able to open with your Excel sheet, 111 00:06:06,200 --> 00:06:09,400 serial_number, name, and age. Not only with that 112 00:06:09,400 --> 00:06:12,400 you can also open this file using some Notepad. 113 00:06:12,400 --> 00:06:14,400 [no audio] 114 00:06:14,400 --> 00:06:16,400 Yes, you are getting. By default 115 00:06:16,400 --> 00:06:19,400 what is the separator you are getting? comma, you are not mentioning 116 00:06:19,500 --> 00:06:23,500 comma to create your CSV file. By default if you don't mention 117 00:06:23,500 --> 00:06:24,600 any delimiter, 118 00:06:25,900 --> 00:06:29,000 then by default your 'csv' module will take delimiter as 119 00:06:29,000 --> 00:06:32,700 comma. Guys, is this is not your delimiter. 120 00:06:32,700 --> 00:06:39,600 This is the separator in your list. This comma and this comma, they 121 00:06:39,600 --> 00:06:40,600 are not equal. 122 00:06:41,700 --> 00:06:42,600 Right. See that. 123 00:06:42,600 --> 00:06:45,800 What I am doing is, now instead of this one, 124 00:06:45,900 --> 00:06:47,900 let me close this file, now 125 00:06:47,900 --> 00:06:49,800 I am going to take delimiter as '|'. 126 00:06:49,800 --> 00:06:51,800 [no audio] 127 00:06:51,800 --> 00:06:56,500 Now save it, and run it, and see the result. Open your file. 128 00:06:56,800 --> 00:07:00,900 I am going to open it using first of all with your 'EditPlus'. 129 00:07:00,900 --> 00:07:02,700 [no audio] 130 00:07:02,700 --> 00:07:06,200 See, now your separator you're getting automatically. Your delimiter 131 00:07:06,200 --> 00:07:09,700 or separator between columns you are getting as '|' 132 00:07:09,700 --> 00:07:10,700 symbol. That's it. 133 00:07:12,000 --> 00:07:15,300 Now, we know that in case you have a '|' symbol to separate 134 00:07:15,300 --> 00:07:16,300 your columns 135 00:07:16,300 --> 00:07:21,500 then your Excel sheet can't open directly, can't separate your 136 00:07:21,800 --> 00:07:25,200 columns. You know, you have to separate it by selecting your 137 00:07:25,200 --> 00:07:30,400 column, then 'Data->Text to Columns', right, then 'Delimited', then 138 00:07:30,400 --> 00:07:33,600 instead of tab or colon or semicolon or comma you have 139 00:07:33,600 --> 00:07:35,300 to provide tab, 140 00:07:35,300 --> 00:07:37,000 sorry '|' symbol, 141 00:07:37,300 --> 00:07:39,400 I mean whatever the delimiter you are using to create, that's 142 00:07:39,400 --> 00:07:41,000 it. So that it will separate. 143 00:07:42,000 --> 00:07:43,000 That's fine. 144 00:07:44,200 --> 00:07:47,000 Now as of now how many rows you are writing? Only header part 145 00:07:47,000 --> 00:07:49,100 you're, you have written here, right? Now 146 00:07:49,100 --> 00:07:51,900 let me take ',' as the separator, you can take anything. 147 00:07:53,100 --> 00:07:56,100 Now I want to write some first row as well. 148 00:07:56,900 --> 00:08:02,400 So just take this object '.writerow', then again provide 149 00:08:02,400 --> 00:08:06,500 a list which consists of suppose, serial_number-1, name- 150 00:08:06,500 --> 00:08:12,500 let's say some 'John', age-let's say 23. Save it and run it. 151 00:08:14,100 --> 00:08:15,600 Go and open your file. 152 00:08:15,600 --> 00:08:19,000 [no audio] 153 00:08:19,000 --> 00:08:21,400 See the output. You have a small confusion here. 154 00:08:22,700 --> 00:08:25,400 Yes, your data is there here but the thing is you are also 155 00:08:25,400 --> 00:08:26,900 getting an empty line here. 156 00:08:28,400 --> 00:08:36,400 Right. So guys if you are working with Python 3 then 157 00:08:36,400 --> 00:08:40,600 let me show you that. Let me close this file. In your script while 158 00:08:40,600 --> 00:08:41,900 opening your file 159 00:08:42,200 --> 00:08:48,600 you have to write 'new_line=""', whereas in Python 2 160 00:08:48,600 --> 00:08:50,500 just write, instead of this no 161 00:08:53,299 --> 00:08:56,100 just write 'wb' mode. 162 00:08:56,100 --> 00:08:59,800 That's it. And this is for Python 2. In Python 3 163 00:08:59,800 --> 00:09:02,000 [no audio] 164 00:09:02,000 --> 00:09:03,100 you have to write in this way. 165 00:09:04,400 --> 00:09:06,000 Now, let me save it and rerun. 166 00:09:07,400 --> 00:09:08,400 What happened? 167 00:09:08,400 --> 00:09:11,500 [no audio] 168 00:09:11,500 --> 00:09:12,500 Sorry. 169 00:09:12,500 --> 00:09:14,700 [no audio] 170 00:09:14,700 --> 00:09:16,300 Yeah, 'newLine'. That's a single word. 171 00:09:16,400 --> 00:09:20,400 Not underscore, right. Now open your file here. 172 00:09:21,200 --> 00:09:22,000 Now see the result. 173 00:09:22,000 --> 00:09:24,900 Now, you don't have any empty lines in your CSV file. 174 00:09:25,700 --> 00:09:26,800 It's working perfectly. 175 00:09:27,100 --> 00:09:29,500 So there is a small difference between Python 3 and 2. 176 00:09:29,500 --> 00:09:30,600 In Python 3 177 00:09:31,300 --> 00:09:32,800 you have to write 'newLine=""', 178 00:09:32,800 --> 00:09:36,800 whereas in Python 2 you have to write simply 'wb', 'write in 179 00:09:36,800 --> 00:09:40,000 binary'. Right. That's fine. 180 00:09:41,100 --> 00:09:44,000 In case if you want to write one more line, I mean one more 181 00:09:44,000 --> 00:09:48,300 row again what you have to take? You have to take 'writer' then 'writerow', 182 00:09:48,300 --> 00:09:51,500 [no audio] 183 00:09:51,500 --> 00:09:55,500 then provide a list, that list consists of your number of 184 00:09:55,500 --> 00:10:01,400 columns, right? Let's say, so this is the data 185 00:10:01,400 --> 00:10:04,100 I want to store. Save it and run it. 186 00:10:05,100 --> 00:10:07,800 Open your file and see the result. 187 00:10:07,800 --> 00:10:12,900 [no audio] 188 00:10:12,900 --> 00:10:18,100 Now, in case, so actually this operation, I mean 'writerow' is 189 00:10:18,100 --> 00:10:22,300 helpful while writing your real-time scripts. 190 00:10:23,400 --> 00:10:26,100 Suppose assume that you are going to work with some multiple servers. 191 00:10:26,400 --> 00:10:30,000 I need to get the content of each and every server like, let's 192 00:10:30,000 --> 00:10:35,800 say my CSV file should be in this way, serial_number, server_name, 193 00:10:35,800 --> 00:10:38,100 and what is the os_name for that, 194 00:10:38,200 --> 00:10:41,900 and what is the up_time for that, right, suppose what is the 195 00:10:42,700 --> 00:10:43,800 version of that. 196 00:10:45,300 --> 00:10:50,200 So 1, serial_number-2, 3, 4; server_name, os_name, what is the 197 00:10:50,200 --> 00:10:51,300 version, what is the up_time, 198 00:10:51,300 --> 00:10:52,400 what are the applications running, 199 00:10:52,400 --> 00:10:53,500 what is the Java version. 200 00:10:55,100 --> 00:10:59,300 Right. I need to write one by one server information into your CSV 201 00:10:59,300 --> 00:11:01,700 file. We will see this type of script while going forward. 202 00:11:01,700 --> 00:11:04,100 [no audio] 203 00:11:04,100 --> 00:11:10,400 Right. So as of now in case if you have a data of each row, if you 204 00:11:10,400 --> 00:11:13,200 want to write that row, you can write by using 'writerow', 205 00:11:13,300 --> 00:11:17,200 'writerow' operation. Instead of that suppose 206 00:11:17,800 --> 00:11:19,900 I want to write at a time all rows, 207 00:11:20,500 --> 00:11:25,800 I want to write at a time all rows into my CSV in a single shot. 208 00:11:25,800 --> 00:11:28,000 [no audio] 209 00:11:28,000 --> 00:11:31,200 Then yeah, you have one more operation called 210 00:11:32,200 --> 00:11:39,400 'writerows', then provide your rows as a list of lists values. 211 00:11:39,400 --> 00:11:41,300 [no audio] 212 00:11:41,300 --> 00:11:46,300 If you remember while printing list of your 'reader' object 213 00:11:46,300 --> 00:11:49,700 what you are getting? You are getting a list which consists 214 00:11:49,700 --> 00:11:53,200 of some number of values and each value is again a list of 215 00:11:53,200 --> 00:11:57,300 values. Right. Now the same way in a reverse way 216 00:11:57,300 --> 00:11:58,800 we are going to write. 217 00:11:59,900 --> 00:12:08,000 See nothing is there. Very simple. Let me take first 'my_data', as I am taking 218 00:12:08,000 --> 00:12:11,900 you can also take in a variable also. 'my_data' a list which 219 00:12:11,900 --> 00:12:16,100 consists of - first value has a list of your header. 220 00:12:17,900 --> 00:12:21,900 Your header has a list as a first value. Second value, 221 00:12:23,300 --> 00:12:28,400 this information again as a list. Second value is also one 222 00:12:28,400 --> 00:12:29,300 row. Row is nothing 223 00:12:29,300 --> 00:12:32,400 but the list. You need to remember that. Then, 224 00:12:37,400 --> 00:12:38,400 That's it. 225 00:12:39,200 --> 00:12:40,900 Now, let me save it, and before that 226 00:12:40,900 --> 00:12:44,100 let me delete our file. Even though if it is there no problem. You 227 00:12:44,100 --> 00:12:47,600 know, if it is in 'write' mode your previous content will be 228 00:12:47,600 --> 00:12:50,400 deleted and new data will be written. 229 00:12:50,400 --> 00:12:53,100 [no audio] 230 00:12:53,100 --> 00:12:55,700 Yeah, now I am going to save it and run it. 231 00:12:57,100 --> 00:12:58,100 What happened? 232 00:12:58,100 --> 00:13:01,900 [no audio] 233 00:13:01,900 --> 00:13:04,800 Oh, sorry 'csv_writer.writerows()'. 234 00:13:05,400 --> 00:13:06,800 So what is your data? 235 00:13:08,100 --> 00:13:13,000 So I have to provide that. I missed that. 'my_data', a list which 236 00:13:13,000 --> 00:13:17,100 consists of values. Each value is like one row. Row is nothing 237 00:13:17,100 --> 00:13:21,600 but a list in your CSV file. That's it. Save it and run it. 238 00:13:22,400 --> 00:13:24,800 Open your location and see the data. Yes, 'demo' 239 00:13:24,800 --> 00:13:27,700 is there. Open it and see the content. 240 00:13:28,800 --> 00:13:32,800 Right. See automatically you are able to get your data here. That's it. 241 00:13:33,100 --> 00:13:36,400 So if you want to store at a time, your all rows, 242 00:13:36,400 --> 00:13:38,700 yes, you can do it using in this way also. 243 00:13:38,700 --> 00:13:41,500 [no audio] 244 00:13:41,500 --> 00:13:46,900 Right. So instead of this if you want to write one by one row, whenever 245 00:13:46,900 --> 00:13:49,200 if you're getting information about suppose particular server, 246 00:13:50,100 --> 00:13:51,600 then you can also use this operation. 247 00:13:52,700 --> 00:13:55,200 Right. So most of the times I will use this operation, 248 00:13:57,000 --> 00:14:00,000 or you can also use this one also. Just to write your data 249 00:14:00,000 --> 00:14:03,300 at at time, all your rows, right? 250 00:14:03,400 --> 00:14:05,600 So based on situation we will use any one of that. 251 00:14:05,900 --> 00:14:09,500 Okay, we will see that one real-time scenario to use effectively 252 00:14:09,500 --> 00:14:13,600 this CSV file while going forward, right. 253 00:14:14,500 --> 00:14:18,200 Okay guys, this is the way how to write a CSV file using 254 00:14:18,200 --> 00:14:22,400 your Python, or how to create a CSV file using your Python. Okay. 255 00:14:22,800 --> 00:14:24,500 Okay, thank you for watching this video. 256 00:14:24,500 --> 00:14:31,500 [no audio]