python导入的.csv数据结尾的java csv 逗号转义能去掉吗(还是去掉数据就不好用啦)

python(21)
CSV模块是Python的内置模块,直接import csv就可调用。csv模块主要就两个函数:csv.reader()——读取csv文件数据,csv.writer()——写入csv文件数据。简单实用。
一、读取CSV
语法:csv.reader(iterable[, dialect='excel'][optional keyword args])
import&csv
reader=csv.reader(open('test.csv',&'rb'))
for&item&in&reader:
&&&&print&line
二、写入CSV
语法:csv.writer(fileobj [, dialect='excel'][optional keyword args])
import&csv
writer=csv.writer(open('test.csv',&'wb'))
writer.writerow(['col1',&'col2',&'col3'])
data=[range(3)&for&i&in&range(3)]
for&item&in&data:
&&&&writer.writerow(item)
须注意之处:writer.writerow()方法中的参数是list类型,如果你想在A1列写入'hello',则必须是writer.writerow(['hello']),不然'hello'会被看成是个list从而被分写入5列。
写入CSV时,CSV文件的创建必须加上'b'参数,即csv.writer(open('test.csv','wb')),不然会出现隔行的现象。网上搜到的解释是:python正常写入文件的时候,每行的结束默认添加‘\n’,即0x0D,而writerow命令的结束会再增加一个0x0D0A,因此对于windows系统来说,就是两行,而采用’b'参数,用二进制进行文件写入,系统默认是不添加0x0D的。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:72798次
积分:1809
积分:1809
排名:第16005名
原创:92篇
转载:26篇
评论:65条
文章:51篇
阅读:41551
(2)(3)(1)(9)(9)(16)(1)(1)(6)(3)(9)(3)(38)(16)(1)追求卓越,成功就会在不经意间追上你!
最近出了一趟差,是从20号去的,今天回来...#
就把最近学习的python内容给大家分享一下...#'''
在python中,(Comma Separated Values),从字面上面理解为:逗号分隔值
举个例子,如:test_csv = 'one, two, three, 4, 5'
对于test_csv这个变量来说,他里面就存放着这样的值:逗号分隔的值。这样的形式
在导入和导出中非常常见,如python(version:3.3.2)的API中所描述的一样:
The so-called (Comma Separated Values) format is the most
common import and export for spreadsheets and databases.
csv模块定义了以下函数:
csv.reader(csvfile, dialect = 'excel', **fmtparams)
Retuen a reader object which will iterate over lines
in the given csvfile.
A short usage example:
import csv
with open('eggs.csv', newline = '') as cf:
spamreader = csv.reader(cf, delimiter = ' ',
quotechar = '|')
for row in spamreader:
print(','.join(row))
csv.write(csvfile, dialect = 'excel', **fmtparams)
Return a writer object reaponsible for converting the
user's data into delimited strings on the given file-like
A short usage example:
import csv
with open('eggs.csv', 'w', newline = '') as cf:
spamwrite = csv.writer(cf, delimiter = ' ', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
一下是我做的demo:
运行效果:
Python 3.3.2 (v3.3.2:df6, May 16 :43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
&&& ================================ RESTART ================================
The path [C:\test] dosen't exist!
Created the path [C:\test]
打开文件:[C:\test\test.csv]
写入数据:['one', 'two', 'three', 'four']
打开文件:[C:\test\test.csv]
读取行:['one,two,three,four']
one,two,three,four
##################################################
打开文件:[C:\test\test.csv]
写入数据:['one', 'two', 'three', 'four']
写入数据:['1', '2', '3']
写入数据:['a', 'b', 'c', 'd']
写入数据:['中国', '美国', '日本', '韩国', '新加坡']
打开文件:[C:\test\test.csv]
读取行:['one,two,three,four']
读取行:['1,2,3']
读取行:['a,b,c,d']
读取行:['中国,美国,日本,韩国,新加坡']
one,two,three,four
中国,美国,日本,韩国,新加坡
在C:\\test目录下面的情况:
====================================================
代码部分:
====================================================
1 #python csv
3 #Author : Hongten
4 #MailTo :
: /hongten
7 #Create :
8 #Version: 1.0
10 import os
11 import csv
在python中,CSV(Comma Separated Values),从字面上面理解为:逗号分隔值
举个例子,如:test_csv = 'one, two, three, 4, 5'
对于test_csv这个变量来说,他里面就存放着这样的值:逗号分隔的值。这样的形式
在导入和导出中非常常见,如python(version:3.3.2)的API中所描述的一样:
The so-called CSV(Comma Separated Values) format is the most
common import and export for spreadsheets and databases.
csv模块定义了以下函数:
csv.reader(csvfile, dialect = 'excel', **fmtparams)
Retuen a reader object which will iterate over lines
in the given csvfile.
A short usage example:
import csv
with open('eggs.csv', newline = '') as cf:
spamreader = csv.reader(cf, delimiter = ' ',
quotechar = '|')
for row in spamreader:
print(','.join(row))
csv.write(csvfile, dialect = 'excel', **fmtparams)
Return a writer object reaponsible for converting the
user's data into delimited strings on the given file-like
A short usage example:
import csv
with open('eggs.csv', 'w', newline = '') as cf:
spamwrite = csv.writer(cf, delimiter = ' ', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
47 #global var
48 SHOW_LOG = True
49 #csv file apth
50 CSV_FILE_PATH = ''
52 def write_data_2_csv_file(path, data):
'''把数据写入到csv文件
这里对要写入的数据进行限制,
数据格式为一个列表:['one', 'two', 'three', 'four']
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
with open(path, 'w', newline = '') as cf:
writer = csv.writer(cf, delimiter = ',', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
if SHOW_LOG:
print('写入数据:{}'.format(data))
writer.writerow(data)
65 def write_datas_2_csv_file(path, datas):
'''把数据写入到csv文件
这里对要写入的数据进行限制,
数据格式为一个列表,列表里面的每一个元素都是一个列表:
['one', 'two', 'three', 'four'],
['1', '2', '3'],
['a', 'b', 'c', 'd']
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
with open(path, 'w', newline = '') as cf:
writer = csv.writer(cf, delimiter = ',', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
for row in datas:
if SHOW_LOG:
print('写入数据:{}'.format(row))
writer.writerow(row)
84 def read_csv_file(path):
'''读取指定的csv文件,并且把csv文件的内容以字符串的形式返回'''
if os.path.exists(path):
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
content = ''
with open(path, newline = '') as cf:
reader = csv.reader(cf, delimiter = ' ', quotechar = '|')
for row in reader:
if SHOW_LOG:
print('读取行:{}'.format(row))
c = ','.join(row) +'\n'
content += c
return content[0:-1]
except csv.Errow as e:
sys.exit('file {}, line {} : {}'.format(path, reader.line_num, e))
print('不存在文件:[{}]'.format(path))
104 def mkdirs(path):
'''创建多级目录'''
if os.path.exists(path):
if SHOW_LOG:
print('The path [{}] existing!'.format(path))
if SHOW_LOG:
print('The path [{}] dosen\'t exist!'.format(path))
os.makedirs(path)
if SHOW_LOG:
print('Created the path [{}]'.format(path))
116 def get_path(absPath):
'''获取到一个绝对路径的目录,
如绝对路径:'C:\\test\\test.csv'
则返回的是'C:\\test'
if os.path.exists(absPath):
if SHOW_LOG:
print('the path [{}] existing!'.format(absPath))
return os.path.split(absPath)[0]
return os.path.split(absPath)[0]
128 def init():
global SHOW_LOG
SHOW_LOG = True
global CSV_FILE_PATH
CSV_FILE_PATH = 'C:\\test\\test.csv'
csv_dir = get_path(CSV_FILE_PATH)
mkdirs(csv_dir)
136 def main():
data = ['one', 'two', 'three', 'four']
['one', 'two', 'three', 'four'],
['1', '2', '3'],
['a', 'b', 'c', 'd'],
['中国', '美国', '日本', '韩国', '新加坡']
write_data_2_csv_file(CSV_FILE_PATH, data)
content = read_csv_file(CSV_FILE_PATH)
print(content)
print('#' * 50)
write_datas_2_csv_file(CSV_FILE_PATH,
content = read_csv_file(CSV_FILE_PATH)
print(content)
154 if __name__ == '__main__':
阅读(...) 评论()87676人阅读
Python(63)
1. 写入并生成csv文件
# coding: utf-8
import csv
csvfile = file('csv_test.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['姓名', '年龄', '电话'])
& & ('小河', '25', '1234567'),
& & ('小芳', '18', '789456')
writer.writerows(data)
csvfile.close()
wb中的w表示写入模式,b是文件模式写入一行用writerow多行用writerows
2. 读取csv文件
# coding: utf-8
import csv
csvfile = file('csv_test.csv', 'rb')
reader = csv.reader(csvfile)
for line in reader:
& & print line
csvfile.close()&
运行结果:
root@he-desktop:~/python/example# python read_csv.py&
['\xe5\xa7\x93\xe5\x90\x8d', '\xe5\xb9\xb4\xe9\xbe\x84', '\xe7\x94\xb5\xe8\xaf\x9d']
['\xe5\xb0\x8f\xe6\xb2\xb3', '25', '1234567']
['\xe5\xb0\x8f\xe8\x8a\xb3', '18', '789456']
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:679833次
积分:6877
积分:6877
排名:第2313名
原创:104篇
译文:101篇
评论:81条
(17)(13)(15)(1)(21)(30)(30)(45)(33)posts - 15,&
comments - 5,&
trackbacks - 0
从CS中导入数据 Python中有一个CSV模块支持读写各种方言格式的CSV文件。方言是很重要的,因为没有一个同意的CSV标准,不同的应用实现CSV的方式略有不同,当看到文件的内容的时候你往往很容易第辨认出文件使用的是哪种方言。 步骤一: 首先导入CSV 模块 步骤二: 然后用with 语句打开数据文件并把它绑定到对象f(with 不用担心操作资源后会关闭数据源,因为with语句的上下文管理器会帮忙处理) 步骤三: 用csv.reader()方法返回reader对象,然后通过该对象遍历所读取文件的所有行。 注意:读取文件一旦出错就会生成错误信息,然后打印出来。 代码贴图:
如果大家还想了解关于csv的处理可以到。如果大家的英文不太好,可以留言,我可一把它翻译出来给大家。
阅读(...) 评论()追求卓越,成功就会在不经意间追上你!
最近出了一趟差,是从20号去的,今天回来...#
就把最近学习的python内容给大家分享一下...#'''
在python中,(Comma Separated Values),从字面上面理解为:逗号分隔值
举个例子,如:test_csv = 'one, two, three, 4, 5'
对于test_csv这个变量来说,他里面就存放着这样的值:逗号分隔的值。这样的形式
在导入和导出中非常常见,如python(version:3.3.2)的API中所描述的一样:
The so-called (Comma Separated Values) format is the most
common import and export for spreadsheets and databases.
csv模块定义了以下函数:
csv.reader(csvfile, dialect = 'excel', **fmtparams)
Retuen a reader object which will iterate over lines
in the given csvfile.
A short usage example:
import csv
with open('eggs.csv', newline = '') as cf:
spamreader = csv.reader(cf, delimiter = ' ',
quotechar = '|')
for row in spamreader:
print(','.join(row))
csv.write(csvfile, dialect = 'excel', **fmtparams)
Return a writer object reaponsible for converting the
user's data into delimited strings on the given file-like
A short usage example:
import csv
with open('eggs.csv', 'w', newline = '') as cf:
spamwrite = csv.writer(cf, delimiter = ' ', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
一下是我做的demo:
运行效果:
Python 3.3.2 (v3.3.2:df6, May 16 :43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
&&& ================================ RESTART ================================
The path [C:\test] dosen't exist!
Created the path [C:\test]
打开文件:[C:\test\test.csv]
写入数据:['one', 'two', 'three', 'four']
打开文件:[C:\test\test.csv]
读取行:['one,two,three,four']
one,two,three,four
##################################################
打开文件:[C:\test\test.csv]
写入数据:['one', 'two', 'three', 'four']
写入数据:['1', '2', '3']
写入数据:['a', 'b', 'c', 'd']
写入数据:['中国', '美国', '日本', '韩国', '新加坡']
打开文件:[C:\test\test.csv]
读取行:['one,two,three,four']
读取行:['1,2,3']
读取行:['a,b,c,d']
读取行:['中国,美国,日本,韩国,新加坡']
one,two,three,four
中国,美国,日本,韩国,新加坡
在C:\\test目录下面的情况:
====================================================
代码部分:
====================================================
1 #python csv
3 #Author : Hongten
4 #MailTo :
: /hongten
7 #Create :
8 #Version: 1.0
10 import os
11 import csv
在python中,CSV(Comma Separated Values),从字面上面理解为:逗号分隔值
举个例子,如:test_csv = 'one, two, three, 4, 5'
对于test_csv这个变量来说,他里面就存放着这样的值:逗号分隔的值。这样的形式
在导入和导出中非常常见,如python(version:3.3.2)的API中所描述的一样:
The so-called CSV(Comma Separated Values) format is the most
common import and export for spreadsheets and databases.
csv模块定义了以下函数:
csv.reader(csvfile, dialect = 'excel', **fmtparams)
Retuen a reader object which will iterate over lines
in the given csvfile.
A short usage example:
import csv
with open('eggs.csv', newline = '') as cf:
spamreader = csv.reader(cf, delimiter = ' ',
quotechar = '|')
for row in spamreader:
print(','.join(row))
csv.write(csvfile, dialect = 'excel', **fmtparams)
Return a writer object reaponsible for converting the
user's data into delimited strings on the given file-like
A short usage example:
import csv
with open('eggs.csv', 'w', newline = '') as cf:
spamwrite = csv.writer(cf, delimiter = ' ', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
47 #global var
48 SHOW_LOG = True
49 #csv file apth
50 CSV_FILE_PATH = ''
52 def write_data_2_csv_file(path, data):
'''把数据写入到csv文件
这里对要写入的数据进行限制,
数据格式为一个列表:['one', 'two', 'three', 'four']
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
with open(path, 'w', newline = '') as cf:
writer = csv.writer(cf, delimiter = ',', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
if SHOW_LOG:
print('写入数据:{}'.format(data))
writer.writerow(data)
65 def write_datas_2_csv_file(path, datas):
'''把数据写入到csv文件
这里对要写入的数据进行限制,
数据格式为一个列表,列表里面的每一个元素都是一个列表:
['one', 'two', 'three', 'four'],
['1', '2', '3'],
['a', 'b', 'c', 'd']
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
with open(path, 'w', newline = '') as cf:
writer = csv.writer(cf, delimiter = ',', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
for row in datas:
if SHOW_LOG:
print('写入数据:{}'.format(row))
writer.writerow(row)
84 def read_csv_file(path):
'''读取指定的csv文件,并且把csv文件的内容以字符串的形式返回'''
if os.path.exists(path):
if SHOW_LOG:
print('打开文件:[{}]'.format(path))
content = ''
with open(path, newline = '') as cf:
reader = csv.reader(cf, delimiter = ' ', quotechar = '|')
for row in reader:
if SHOW_LOG:
print('读取行:{}'.format(row))
c = ','.join(row) +'\n'
content += c
return content[0:-1]
except csv.Errow as e:
sys.exit('file {}, line {} : {}'.format(path, reader.line_num, e))
print('不存在文件:[{}]'.format(path))
104 def mkdirs(path):
'''创建多级目录'''
if os.path.exists(path):
if SHOW_LOG:
print('The path [{}] existing!'.format(path))
if SHOW_LOG:
print('The path [{}] dosen\'t exist!'.format(path))
os.makedirs(path)
if SHOW_LOG:
print('Created the path [{}]'.format(path))
116 def get_path(absPath):
'''获取到一个绝对路径的目录,
如绝对路径:'C:\\test\\test.csv'
则返回的是'C:\\test'
if os.path.exists(absPath):
if SHOW_LOG:
print('the path [{}] existing!'.format(absPath))
return os.path.split(absPath)[0]
return os.path.split(absPath)[0]
128 def init():
global SHOW_LOG
SHOW_LOG = True
global CSV_FILE_PATH
CSV_FILE_PATH = 'C:\\test\\test.csv'
csv_dir = get_path(CSV_FILE_PATH)
mkdirs(csv_dir)
136 def main():
data = ['one', 'two', 'three', 'four']
['one', 'two', 'three', 'four'],
['1', '2', '3'],
['a', 'b', 'c', 'd'],
['中国', '美国', '日本', '韩国', '新加坡']
write_data_2_csv_file(CSV_FILE_PATH, data)
content = read_csv_file(CSV_FILE_PATH)
print(content)
print('#' * 50)
write_datas_2_csv_file(CSV_FILE_PATH,
content = read_csv_file(CSV_FILE_PATH)
print(content)
154 if __name__ == '__main__':
阅读(...) 评论()

我要回帖

更多关于 csv 逗号 转义 的文章

 

随机推荐