How to handle csv file using python

As i need to read something from a csv file using python.  I got something and put it here.

Module: csv

import csv

FILE_FULL_PATH = ‘D:\\Work\\script\\My Script\\Book1.csv‘

def f():
  with open(FILE_FULL_PATH,‘rb‘) as csvfile:
    for row in csv.reader(csvfile, delimiter=‘ ‘, quotechar=‘|‘):
    print type(row)
      for eachcol in row:
      print eachco

if __name__ == ‘__main__‘:
f()

OUTPUT:

C:\Python27\python.exe "D:/Work/script/My Script/my-test.py"
<type ‘list‘>  #here we know each row is read as a list
Col11,Col12,Col12,,COlE  #The col which is empty is stored as a empty in the list
<type ‘list‘>  
Col21,Col22,Col23,,

Process finished with exit code 0

how to write to csv file

def f_w():
  datas = []  #using list to store data
  with open(CONFIG_FILE_PATH,‘r‘) as f:       #read data from a txt file
  FIRST = True
  for line in f:
  if FIRST:
    datas.append(re.findall(‘[a-zA-Z]+‘,line))    #find word and retuan a list
    FIRST = False
  else:
    datas.append(re.findall(‘[a-zA-Z_ 0-9]+‘,line))   #as the data has _and 0-9
  for data in datas:
  print data,len(data)
  with open(FILE_FULL_PATH,‘wb‘) as csvfile:   #open file need to using b mode
    csvwriter = csv.writer(csvfile)     #converte to csvwriter
    for data in datas:                       #for each line in list write to csv
    csvwriter.writerow(data)

时间: 2024-12-11 09:57:47

How to handle csv file using python的相关文章

CSV文件在Python中的几种处理方式

Comma Separated Values,简称CSV,它是一种以逗号分隔数值的文件类型.在数据库或电子表格中,它是最常见的导入导出格式,它以一种简单而明了的方式存储和共享数据,CSV文件通常以纯文本的方式存储数据表.今天,我将给大家分享在Python中如何操作CSV文件. 一.数据源 首先,我们来看看本次操作的数据源,图1 CSV文件是在Excel中打开的,图2 CSV文件是在Notepad++中打开的,我们在图2中可以看到数值之间是以逗号分隔开的,每行末尾是CR回车符和LF换行符(请注意,

C# - CSV file reader

// -------------------------------------------------------------------------------------------------------------------- // <summary> // Defines the CSVFileReader type. // </summary> // ----------------------------------------------------------

a helper class for generating xls or csv file

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Office.Interop.Excel; namespace Reuters.ProcessQuality.ContentAuto.Lib{ public class XlsOrCsvUtil { /// <summary> /// Generate Xls Or Csv File /// <

解决Jmeter Non GUI运行时报“...ensure the jmeter .save.saveservice.* properties are the same as when the CSV file was created or the file may be read incorrectly”错误的问题

错误信息: File 'xxx.jtl' does not contain the field names header, ensure the jmeter.save.saveservice.* properties are the same as when the CSV file was created orthe file may be read incorrectly 解决: jmeter.properties配置文件 jmeter.save.saveservice.output_fo

[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

Hello everyone, this is the third post of the series. .   Background =============== In my solution, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Withou

webpack 3.8 使用 extract-text-webpack-plugin 3.0 抽取css失败:You may need an appropriate loader to handle this file type.

webpack 3.8.1 使用 extract-text-webpack-plugin 3.0.2 抽取css时失败,报错: ERROR in ./src/static/style/localTime.css Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. | .localTimeBox { | color: red; | } @ .

Powercli随笔 - PowerCLI script to sequentially Storage vMotion VMs from a CSV File

PowerCLI script to sequentially Storage vMotion VMs from a CSV File This is a PowerCLI script that I use to Storage vMotion (s/vmotion) VMs from an input file (CSV File). This helps me evacuate VMs from a datastore that will be decommissioned and thi

[Python] Read and plot data from csv file

Install: pip install pandas pip install matplotlib # check out the doc from site import pandas as pd import matplotlib.pyplot as plt from numpy import mean def load_df(symbol): return pd.read_csv("data/{0}.csv".format(symbol)) def get_max_close(

Csharp--Read Csv file to DataTable

在网上找的资料都不怎么好使,许多代码一看就知道根本没有考虑全面. 最后找到一个好用的,在codeplex上,这位老兄写成了一个framework,太重了. http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader 确实挺好用的. 我没耐下性子看他的实现,自己尝试写了如下的代码来完成了阅读csv. 参照:http://msdn.microsoft.com/en-us/library/ae5bf541%28v=vs.90%29.aspx 只