How do I work with CSV files using python?

Source: Internet
Author: User
Tags jupyter jupyter notebook

This article and we share the main is to use the Python language to deal with the CSV file related content, come together to see it, I hope that you learn Python development helpful.

CSV (comma-separated values) is a comma-separated value that can be opened for viewing in Excel . Because it is plain text, any editor can also be opened. Unlike the Excel file,theCSV file:

· value has no type, all values are string

· styles such as font color cannot be specified

· cannot specify a cell's width height, cannot merge cells

· No More worksheets

· cannot embed image chart

in the CSV file, separated by two cells, as separators. Like this, a,,c indicates that There is a blank cell between cell a and cell C . And so on

Not every comma represents the demarcation between cells. So even if the CSV is a plain text file, it is also insisted on using specialized modules for processing. Python has a csv module built in. Let's take a look at a simple example.

reading data from a CSV file

Import CSV

filename = ' f:/jupyter notebook/matplotlib_pygal_csv_json/sitka_weather_2014.csv 'with open ( FileName) as F:

reader = Csv.reader (f)

print (List reader)

data cannot be printed directly,the outermost layer of list (data) is list, and each row in the inside is a list It's kind of like this .

[[' Name ', ' age '], [' Bob ', ' + '], [' Tom ', ' 23 '], ...]

so we can access the Bob 's age reader[1][1] , traversing the For loop as follows

Import CSV

filename = ' f:/jupyter notebook/matplotlib_pygal_csv_json/sitka_weather_2014.csv 'with open ( FileName) as F:

reader = Csv.reader (f)

for row in reader:

# line number starting from 1

Print (reader.line_num, row)

It is important to note thatreader can only be traversed once. Because reader is an iterative object, you can use the next method to get one row at a time.

Import CSV

filename = ' f:/jupyter notebook/matplotlib_pygal_csv_json/sitka_weather_2014.csv ' with open ( FileName) as F:

reader = Csv.reader (f)

# Read a line, there is no line in the reader below

Head_row = Next (reader)

for row in reader:

# line number starting from 2

Print (reader.line_num, row)

write data to a csv file

there is reader to read, and of course writer can write. Write one line at a time, and write more than one line at a time.

Import CSV

# numbers and strings can be used

datas = [[' Name ', ' age '],

[' Bob ', 14],

[' Tom ', 23],

[' Jerry ', ' 18 ']

with open (' Example.csv ', ' W ', newline= ') as F:

writer = Csv.writer (f)

for row in datas:

writer.writerow (Row)

# You can also write multiple lines

writer.writerows (datas)

If you do not specify newline= ' , a blank line is written to each write line. The above code generates the following.

name, age

bob,14

tom,23

jerry,18name, age

bob,14

tom,23

jerry,18

dictreader and dictwriter objects

Use Dictreader can manipulate the data like a dictionary, putting the first row of the table (usually the header) as key. Use key to access the data for that key in the row .

Import CSV

filename = ' f:/jupyter notebook/matplotlib_pygal_csv_json/sitka_weather_2014.csv ' with open ( FileName) as F:

reader = csv. Dictreader (f)

for row in reader:

# Max Temperaturef is a data from the first row of the table, as key

max_temp = row[' Max Temperaturef ']

Print (max_temp)

Use The Dictwriter class can write data in the form of a dictionary, and the same key is also the header (the first row of the table).

Import CSV

headers = [' name ', ' age ']

datas = [{' Name ': ' Bob ', ' age ': 23},

{' name ': ' Jerry ', ' Age ': 44},

{' name ': ' Tom ', ' Age ': 15}

]

with open (' Example.csv ', ' W ', newline= ') as F:

# The header is passed here as the first row of data

writer = csv. Dictwriter (f, headers)

Writer.writeheader ()

for row in datas:

writer.writerow (Row)

# You can also write multiple lines

writer.writerows (datas)

Source: Blog Park


How do I work with CSV files using python?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.