前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python3.x 读写csv文件中的数字

Python3.x 读写csv文件中的数字

作者头像
AnRFDev
发布2021-02-01 15:31:52
2.9K0
发布2021-02-01 15:31:52
举报
文章被收录于专栏:AnRFDevAnRFDev

Win7 Python3.6

读写csv文件

读文件时先产生str的列表,把最后的换行符删掉;然后一个个str转换成int

代码语言:javascript
复制
## 读写csv文件
csv_file = 'datas.csv'

csv = open(csv_file,'w')
for i in range(1,20):
    csv.write(str(i) + ',')
    if i % 10 == 0:
        csv.write('\n')
csv.close()

result = []
with open(csv_file,'r') as f:
    for line in f:
        linelist = line.split(',')
        linelist.pop()# delete: \n
        for index, item in enumerate(linelist):
            result.append(int(item))
print('\nResult is \n' , result)

输出:

代码语言:javascript
复制
Result is
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

检查目录是否存在

若目标目录不存在,则新建一个目录

代码语言:javascript
复制
import os
json_dir = "../dir_json/2017-04/"
if not os.path.exists(json_dir):
    print("json dir not found")
    os.makedirs(json_dir)
    print("Create dir  " + json_dir)

写文件时指定格式

参考下面的代码,打开文件时指定utf8,转换成json时指定ensure_ascii=False

代码语言:javascript
复制
import json
json_file = open(json_dir + id + '.json', 'w', encoding='utf8')
json_file.write(json.dumps(data_dict, ensure_ascii=False))

避免写成的json文件乱码

函数 enumerate(iterable, start=0)

返回一个enumerate对象。iterable必须是一个句子,迭代器或者支持迭代的对象。

enumerate示例1:

代码语言:javascript
复制
>>> data = [1,2,3]
>>> for i, item in enumerate(data):
	print(i,item)


0 1
1 2
2 3

示例2:

代码语言:javascript
复制
>>> line = 'one'
>>> for i, item in enumerate(line,4):
	print(i,item)


4 o
5 n
6 e

参考: https://docs.python.org/3/library/functions.html?highlight=enumerate#enumerate

class int(x=0)

class int(x, base=10) 返回一个Integer对象。对于浮点数,会截取成整数。

代码语言:javascript
复制
>>> print(int('-100'),int('0'),int('3'))
-100 0 3
>>> int(7788)
7788
>>> int(7.98)
7
>>> int('2.33')
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    int('2.33')
ValueError: invalid literal for int() with base 10: '2.33'

读取binary文件

逐个byte读取,注意用b''来判断是否读到文件尾部

代码语言:javascript
复制
@staticmethod
def convert_bin_to_csv(bin_file_path, csv_file_path):
    if not os.path.exists(bin_file_path):
        print("Binary file is not exist! " + bin_file_path)
        return
    with open(bin_file_path, "rb") as bin_f:
        cur_byte = bin_f.read(1)
        while cur_byte != b'':
            # Do stuff with byte.
            print(int.from_bytes(cur_byte, byteorder='big', signed=True))
            cur_byte = bin_f.read(1)

读取到的byte可以转换为int,参考文档

这里 cur_byte 类似于 b'\x08'

代码语言:javascript
复制
print(int.from_bytes(cur_byte, byteorder='big', signed=True))

从bin中读取数据并存入CSV文件中

先从bin中读取byte,规定好几个字节凑成1个数字。 按每行一个数字的格式写入CSV文件。

代码语言:javascript
复制
@staticmethod
def convert_bin_to_csv(bin_file_path, csv_file_path, byte_count=1, byte_order='big', digit_signed=True):
    if not os.path.exists(bin_file_path):
        print("Binary file is not exist! " + bin_file_path)
        return
    with open(csv_file_path, "w") as csv_f:
        with open(bin_file_path, "rb") as bin_f:
            cur_byte = bin_f.read(byte_count)
            while cur_byte != b'':
                csv_f.write(str(int.from_bytes(cur_byte, byteorder=byte_order, signed=digit_signed)) + ",\n")
                cur_byte = bin_f.read(byte_count)

bin存储的数据格式一定要商量好。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-12-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 读写csv文件
    • 检查目录是否存在
      • 写文件时指定格式
        • 函数 enumerate(iterable, start=0)
          • class int(x=0)
          • 读取binary文件
            • 从bin中读取数据并存入CSV文件中
            相关产品与服务
            文件存储
            文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档