文件读写的csv操作

import csv

rows =[['张三',13],['李三',14],['王三',15]]

#with open('code.text','w',encoding='utf-8')as f:

csv文件在写入的时候  默认每次写入会有一个空行作为分割

使用newline='' 可以将空行去掉

  1. with open  ('test1.csv','w',newline='')as csv_file:

  #获取一个csv对象进行内容写入

                  writer = csv.writer(csv_file)

                  for row in rows:

  #writerow 写入一行数据

                        writer.writerow(row)

     2.with open ('test1.csv','r')as read_file:

       #获取一个csv对象进行内容读取

                   reader = csv.reader(read_file)

                   print([row for row in reader[)

     3.def write_data():

                columns = int(input('请输入总列数'))

                col_list = []

                while True:

                       col_list.append([input('请输入第{}列数据'.format(n + 1))for n in range(columns)])

                       is_continue != 'Y'

                              break

                             #此处不可用continue和ruturn

                             #continue 跳出本次循环 下次循环继续执行

                             #return 结束循环和方法 return后面的代码统统不执行

                 print(col_list)

                 with open('test2.csv','w',newline='') as csv_file:

                          writer = csv.writer(csv_file)

                          for row in col_list:

                                writer.writerow(row)

        write_data()

      4.data_dic = [{'name':'zhangsan','age':'15'},{'name':'lisi','age':'25'}]

         with open('dict.csv','w',newline='') as csv_file:

                  keys = []

                  for key in data_dic[0].keys():

                         print(key)

                         keys.append(key)

                  #写入一个字典到csv中(excel) fieldnames 设置文本的标题

                  writer = csv.DictWriter(csv_file,fieldnames=keys)

                  #开始和写入标题

                  writer.writeheader()

                  for dict in data_dic:

                          writer.writerow(dict)

      5.with open('dict.csv','r') as csv_file:

                 reader = csv.DictReader(csv_file)

                 print([row for row in reader])

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Boost库中的Boost.IOStreams模块来读写CSV文件。以下是一个简单的示例代码,演示了如何使用Boost来读取和写入CSV文件。 ```cpp #include <boost/iostreams/device/file.hpp> #include <boost/iostreams/stream.hpp> #include <boost/tokenizer.hpp> #include <iostream> #include <string> using namespace boost::iostreams; using namespace std; int main() { // 读取CSV文件 ifstream file("data.csv"); if (!file) { cout << "无法打开文件" << endl; return 1; } // 创建一个输入流 stream<file_source> input(file); // 逐行读取并解析CSV数据 string line; while (getline(input, line)) { tokenizer<escaped_list_separator<char>> tok(line); for (const auto &token : tok) { cout << token << "|"; // 在这里对每个单元格执行适当的操作 } cout << endl; } // 写入CSV文件 ofstream outputFile("output.csv"); if (!outputFile) { cout << "无法创建文件" << endl; return 1; } // 创建一个输出流 stream<file_sink> output(outputFile); // 写入CSV数据 output << "列1,列2,列3" << endl; output << "1,2,3" << endl; output << "4,5,6" << endl; return 0; } ``` 上述代码中,我们首先使用`ifstream`打开要读取的CSV文件,并将其传递给`stream<file_source>`以创建输入流。然后,我们使用`getline`逐行读取CSV数据,并使用Boost的`tokenizer`将每行分解为单元格。您可以在`for`循环中对每个单元格执行适当的操作。 接下来,我们使用`ofstream`创建要写入的CSV文件,并将其传递给`stream<file_sink>`以创建输出流。然后,我们使用输出流来写入CSV数据。 请注意,您需要在编译时链接Boost库,并确保您已经正确安装了Boost库。 希望这可以帮助到您!如果您有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值