在Python中读取和写入CSV文件中的数据 [英] Reading and Writing data in CSV files in Python

查看:96
本文介绍了在Python中读取和写入CSV文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,我在Python中使用CSV文件时遇到了一些问题.请帮帮我

I am new to Python and I am having some problems with CSV files in Python. Please help me out

  1. 如何在Python中打开和读取存在于其他目录中的csv文件?我知道我们可以做到

  1. How do I open and read csv files in Python which are present in some other directory? I know we can do


import csv 
f = open('attendees1.csv')

只要我的程序文件和csv文件在同一目录中即可.但是,如何提供指向另一个目录中的csv文件的链接?

As long as my program file and csv file is in the same directory. But how do I provide a link to the csv file sitting in another directory?

我有一个包含多行和多列的列表,如何将这些数据传输到csv文件并将其保存在特定位置?

I have a list with multiple rows and columns, how do I transfer this data to a csv file and save it down in a particular location?

请帮帮我

推荐答案

open() 是文件,可以是诸如C:\Program Files\file.csv的绝对路径,也可以是诸如../../file.csv的相对路径,此处..指当前目录上方的目录,而.指当前目录

First argument of open() is file, which can be an absolute path like C:\Program Files\file.csv or a relative one like ../../file.csv here .. refers to the directory above the current directory and . refers to the current directory.

import csv
with open('../path/to/file.csv', 'w') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerows(your_row_data)

your_row_data是列表列表.

这篇关于在Python中读取和写入CSV文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆