Simple use of the () method
Example code 1:
import csv f = open('sample','r',encoding='utf8') reader = (f) print(reader) # < object at 0x000002241D730FD0> for line in reader: # reader for ease of understanding we can think of it as a list nested OrderedDict (a list-like data type) print(line) # OrderedDict([('id', '1'), ('name', 'jason'), ('age', '18')])
sample is a txt file with the following contents:
id,name,age 1,jason,18 2,jian,20 3,xiaoming,30 4,dog,40
The code is run and the output in the terminal is:
< object at 0x000001FCF6FA0FD0> # from sample code 1 in the print(reader)
OrderedDict([('id', '1'), ('name', 'jason'), ('age', '18')]) # from the sample code 1 in the print(line)
1 jason 18 # from the sample code 1 in the print (line ['id'], line ['name'], line ['age'])
OrderedDict([('id', '2'), ('name', 'jian'), ('age', '20')])
2 jian 20
OrderedDict([('id', '3'), ('name', 'xiaoming'), ('age', '30')])
3 xiaoming 30
OrderedDict([('id', '4'), ('name', 'dog'), ('age', '40')])
4 dog 40
OrderedDict is a datatype that looks similar to a list that has tuples nested within it Example: line = OrderedDict([('id', '1'), ('name', 'jason'), ('age', '18')]), where the first element in each tuple is the key and the second element is the value (similar to a dictionary), and each Where do the keys in the tuple come from? == By default (which you can set yourself) the first line of data read by ==() is the key. And it is possible to take out values from the OrderedDict data by means of indexing print(line['id'],line['name'],line['age']) # It is possible to take out values by means of indexing by key (similar to a dictionary).
Using the fieldnames parameter of ()
In reader = (f,fieldnames=['new_id','new_name','new_age']) add the arguments fieldnames=['new_id','new_name','new_age'] to specify the key.
Example code 2:
import csv f = open('sample','r',encoding='utf8') # Specify fields by fieldnames parameter reader = (f,fieldnames=['new_id','new_name','new_age']) head_row = next(reader) # next() method is used to move the pointer print(reader) # < object at 0x000002241D730FD0> for line in reader: # reader for ease of understanding we can think of it as a list nested OrderedDict (a list-like data type) print(line) # OrderedDict([('new_id', '2'), ('new_name', 'jian'), ('new_age', '20')]) # Index fetch and printout by specified fields print(line['new_id'],line['new_name'],line['new_age']) # You can index values by key(Dictionary-like)
next () method used to move the pointer, sample code 2 in the head_row = next (reader) to obtain the first line of data stored in the head_row, the implementation of a next () the pointer to move a line, this time the pointer has moved to the beginning of the second line, and then read the data from the second line of the beginning of reading. If you do not perform head_row = next(reader) then the output will also be more such results OrderedDict([('new_id', 'id'), ('new_name', 'name'), ('new_age', 'age')]) the first line of data is also counted in it.
The code is run and the output in the terminal is:
< object at 0x000001D329CF2080> # from sample code 2 print(reader)
OrderedDict([('new_id', '1'), ('new_name', 'jason'), ('new_age', '18')]) # from the sample code 2 of the print(line)
1 jason 18 # from sample code 2 print(line['new_id'], line['new_name'], line['new_age'])
OrderedDict([('new_id', '2'), ('new_name', 'jian'), ('new_age', '20')])
2 jian 20
OrderedDict([('new_id', '3'), ('new_name', 'xiaoming'), ('new_age', '30')])
3 xiaoming 30
OrderedDict([('new_id', '4'), ('new_name', 'dog'), ('new_age', '40')])
4 dog 40
restkey argument to ()
If you read a row that has more values than the sequence of key names, the remaining data is added as a value under the key in the restkey. At this point we modify the sample file to add one more column of data.
In reader = (f,fieldnames=['new_id','new_name','new_age'],restkey='hobby') add restkey='hobby' to specify the key that receives the extra value, and note that restkey can only be passed in as a value, not as a list, a tuple, or a string. data types.
sample is a txt file with the following contents:
id,name,age 1,jason,18,dbj 2,jian,20,lol 3,xiaoming,30,game 4,dog,40,noting
Sample code 3:
import csv f = open('sample','r',encoding='utf8') # Specify the field through the fieldnames parameter, values exceeding the number of keys in the fieldnames will be received by the key in the restkey. reader = (f,fieldnames=['new_id','new_name','new_age'],restkey='hobby') head_row = next(reader) # next is used to move the pointer print(reader) # < object at 0x000002241D730FD0> for line in reader: # reader for ease of understanding we can think of it as a list nested OrderedDict (a list-like data type) print(line) # OrderedDict([('new_id', '2'), ('new_name', 'jian'), ('new_age', '20')]) # Index fetch and printout by specified fields print(line['new_id'],line['new_name'],line['new_age'],line['hobby']) # You can index values by key(Dictionary-like)
The code is run and the output in the terminal is:
< object at 0x000001CB6B6030F0> # from sample code 3 print(reader)
OrderedDict([('new_id', '1'), ('new_name', 'jason'), ('new_age', '18'), ('hobby', ['dbj'])]) # from the sample code 3 of the print(line)
1 jason 18 # from the sample code 3 print(line['new_id'], line['new_name'], line['new_age'])
OrderedDict([('new_id', '2'), ('new_name', 'jian'), ('new_age', '20'), ('hobby', ['lol'])])
2 jian 20
OrderedDict([('new_id', '3'), ('new_name', 'xiaoming'), ('new_age', '30'), ('hobby', ['game'])])
3 xiaoming 30
OrderedDict([('new_id', '4'), ('new_name', 'dog'), ('new_age', '40'), ('hobby', ['noting'])])
4 dog 40
From the results of the code run we will see that the extra value is indeed received using the key restkey='hobby' specified by OrderedDict([('new_id', '1'), ('new_name', 'jason'), ('new_age', '18'), ('hobby', [' dbj'])])
Note that although the extra key can be picked up with the key specified by restkey, it cannot be printed out by index, i.e., if you execute print(line["hobby"]), you will get the error KeyError: 'hobby'.
Using the restval parameter of ()
If the read rows have fewer values than the sequence of key names, then the remaining keys will use the values in the optional restval parameter. In this case, we modify the sample file to add one more column of data.
In reader = (f,fieldnames=['new_id','new_name','new_age','hobby'],restval='others') add restval='others' to specify the default value when the corresponding value of the key is null and note that restval can only be passed as a value. and it should be noted that restval can only be passed as a value, not as a list or tuple data type.
sample is a txt file with the following contents:
id,name,age 1,jason,18 2,jian,20,lol 3,xiaoming,30 4,dog,40,noting
Sample code 4:
import csv f = open('sample','r',encoding='utf8') # Specify the field through the fieldnames parameter, values exceeding the number of keys in the fieldnames will be received by the key in the restkey. reader = (f,fieldnames=['new_id','new_name','new_age','hobby'],restval='others') head_row = next(reader) # next is used to move the pointer # print(reader) # < object at 0x000002241D730FD0> for line in reader: # reader for ease of understanding we can think of it as a list nested OrderedDict (a list-like data type) print(line) # OrderedDict([('new_id', '1'), ('new_name', 'jason'), ('new_age', '18'), ('hobby', 'others')]) # Index fetch and printout by specified fields print(line['new_id'],line['new_name'],line['new_age'],line['hobby']) # You can index values by key(Dictionary-like)
The code is run and the output in the terminal is:
OrderedDict([('new_id', '1'), ('new_name', 'jason'), ('new_age', '18'), ('hobby', 'others')]) # from the sample code 4 print(line)
1 jason 18 others # from the sample code 4 print(line['new_id'], line['new_name'], line['new_age'], line['hobby'])
OrderedDict([('new_id', '2'), ('new_name', 'jian'), ('new_age', '20'), ('hobby', 'lol')])
2 jian 20 lol
OrderedDict([('new_id', '3'), ('new_name', 'xiaoming'), ('new_age', '30'), ('hobby', 'others')])3 xiaoming 30 others
OrderedDict([('new_id', '4'), ('new_name', 'dog'), ('new_age', '40'), ('hobby', 'noting')])
4 dog 40 noting
to this article on the python operation of csv format files () method of the article is introduced to this, more related python method of content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!