Python csv 的文件读取

来源:互联网 发布:游子吟永恒在召唤知乎 编辑:程序博客网 时间:2024/04/28 08:23

环境:

Python 3.6.0 |Anaconda 4.3.1 (32-bit)| (default, Dec 23 2016, 12:06:52) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.


详细内容见:https://docs.python.org/3.6/library/csv.html


首先根据

import csvwith open('a.csv',newline='') as csvfile:spamreader = csv.reader(csvfile,delimiter=' ',quotechar='|')for row in spamreader:print (','.join(row))

在cmd运行后:

运行后会提示错误:

FileNotFoundError: [Errno 2] No such file or directory: 'a.csv'


网上找了下原因:

You are using a relative path, which means that the program looks for the file in the working directory. The error is telling you that there is no file of that name in the working directory.

Try using the exact, or absolute, path.

也就是说,Python读取的文件是他的工作环境里的文件,而不是相对路径的文件。

为了找到文件路径:

import oscwd = os.getcwd()  # Get the current working directory (cwd)files = os.listdir(cwd)  # Get all the files in that directoryprint("Files in '%s': %s" % (cwd, files))

在CMD拖入py文件:

得C:\Users\lenovo>F:\Cocoon\DA项目\Cocoon.py
Files in 'C:\Users\lenovo':[省略...]

然后就明白了自己cmd工作路径没有设置好,要设置在写的python文件夹上。

在cmd中改变路径: F:\Cocoon\DA项目>F:\Cocoon\DA项目\Cocoon.py

Files in 'F:\Cocoon\DA项目': ['a.csv', 'chinavis', 'Cocoon.py', 'crime DA']


接下来再运行原代码:

UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 111: illegal multibyte sequence


这里转解决方案:http://www.crifan.com/summary_python_unicodedecode_error_possible_reasons_and_solutions/


解码出错,也就是说csv文件里出现了非法字符,超出了gbk的编码范围。

换了一个csv文件发现可以读取,这就是读取方式的问题了。


import csvwith open('a.csv',newline='',encoding='UTF-8') as csvfile:spamreader = csv.reader(csvfile,delimiter=' ',quotechar='|')for row in spamreader:print (','.join(row))



转换成UTF-8就读取完成了


接下来做了时间戳转时间:

import csvimport stringimport timecsvfile = open('csvtest1.csv','w',encoding='UTF-8',newline='')nodes = csv.writer(csvfile)nodes.writerow(['md5','content','phone','conntime','recitime','lng','lat'])l=[]with open('a.csv',newline='',encoding='UTF-8') as f:reader = csv.DictReader(f)for row in reader:if row['md5'] == 'md5':continueelse:timeStamp1 = float(row['recitime'])/1000timeArray1 = time.localtime(timeStamp1)otherStyleTime1 = time.strftime("%Y-%m-%d %H:%M:%S",timeArray1)print (otherStyleTime1)timeStamp2 = float(row['recitime'])/1000timeArray2 = time.localtime(timeStamp2)otherStyleTime2 = time.strftime("%Y-%m-%d %H:%M:%S",timeArray2)print (otherStyleTime2)l.append([row['md5'],row['content'],row['phone'],otherStyleTime1,otherStyleTime2,row['lng'],row['lat']])nodes.writerows(l)csvfile.close()


0 0
原创粉丝点击