原文地址:https://blog.csdn.net/weixin_42555131/article/details/82012642

生成txt文件

mesg = "hello world"

with open("test.txt", "w") as f:
f.write("{}".format(mesg))
print("加载完成!")

生成json文件

import json

mesg = {"key": "value"}

with open("test.json", "w") as f:
json.dump(mesg, f)
print("加载完成!")
 

生成csv文件

import csv

with open("test.csv", "w") as f:
fieldnames = ["name", "age"] # 表的列名
writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() # 加上表头
writer.writerow({"name": "shannon-li", "age": 4}) # 按行添加
print("加载完成!")

打开txt文件

with open("test.txt") as f:
content = f.read()
print("文件内容:{}".format(content))

打开json文件

import json
import sys with open("test.json") as f:
try:
content = json.load(f)
print("文件内容:{}".format(content))
except TypeError:
sys.exit("Error on load json file.")

打开csv文件

import csv
import sys content = [] with open("test.csv") as f:
reader = csv.DictReader(f, delimiter=",", quotechar="|") try:
for row in reader:
content.append({"name": row["name"], "age": row["age"]})
print("文件内容:".format(content))
except csv.Error as e:
sys.exit("file %s, line %d: %s" % (f, reader.line_num, e))

--------------------- 本文来自 shannon-Li 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/weixin_42555131/article/details/82012642?utm_source=copy

转载:python生成以及打开json、csv和txt文件的更多相关文章

  1. Python生成PASCAL VOC格式的xml标注文件

    Python生成PASCAL VOC格式的xml标注文件 PASCAL VOC数据集的标注文件是xml格式的.对于py-faster-rcnn,通常以下示例的字段是合适的: <annotatio ...

  2. 将excel中某列数据中,含有指定字符串的记录取出,并生成用这个字符串命名的txt文件

    Python 一大重要的功能,就是可处理大量数据,那分不开的即是使用Excel表格了,这里我做下学习之后的总结,望对我,及广大同仁们是一个帮助Python处理Excel数据需要用到2个库:xlwt 和 ...

  3. 面试题-python 如何读取一个大于 10G 的txt文件?

    前言 用python 读取一个大于10G 的文件,自己电脑只有8G内存,一运行就报内存溢出:MemoryError python 如何用open函数读取大文件呢? 读取大文件 首先可以自己先制作一个大 ...

  4. C#生成PDF文档,读取TXT文件内容

    using System.IO;using iTextSharp.text;using iTextSharp.text.pdf; //需要在项目里引用ICSharpCode.SharpZipLib.d ...

  5. excel、csv、txt文件数据读取

    /// <summary> /// 读取Excel表每一行第一列的字符串集合 /// </summary> /// <param name="filePath& ...

  6. 【应用】:shell crontab定时生成oracle表的数据到txt文件,并上传到ftp

    一.本人环境描述      1.oracle服务端装在win7 32位上,oracle版本为10.2.0.1.0      2.Linux为centos6.5 32位,安装在Oracle VM Vir ...

  7. BULK INSERT将CSV或TXT文件导入到SQL Server

    CSV代表逗号分隔值,有时也被称为逗号分隔的值.而 如果加载txt文件,然后文件应该有逗号分隔的值.和文件应该是这样 下面是该脚本以创建表: CREATE TABLE Employee( Id int ...

  8. py3 读入和写入csv,txt文件

    import numpy as npimport pandas as pdimport time import datetimeimport csv http://pandas.pydata.org/ ...

  9. python 检索一个目录下所有的txt文件,并把文件改为.log

    检索一个目录及子目录下所有的txt文件,并把txt文件后缀改为log: import os f_path = r'C:\Users\PycharmProjects\mystudy\Testfolder ...

随机推荐

  1. python高级-异常(13)

    一.异常介绍 print("1---------------------") open("123.txt","r") print(" ...

  2. 【shiro】(3)---了解Shiro

    了解Shiro 一Apache Shiro作用 Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理,令行应用程序. Shiro提供了应用程序安全A ...

  3. stack源码

    stack概述 stack是一种先进后出的数据结构,它只有一个出口,允许新增元素.移除元素.取得最顶端元素,但每次只能处理顶端元素,也就是说,stack不允许遍历行为. stack定义 以某种既有容器 ...

  4. 比较 Spring AOP 与 AspectJ

    本文翻译自博客Comparing Spring AOP and AspectJ(转载:https://juejin.im/post/5a695b3cf265da3e47449471) 介绍 如今有多个 ...

  5. 关于return的分号自动插入问题

    在<JavaScript语言精粹>这本书里,这个“自动插入分号”机制被划入到了JavaScript的毒瘤里面,与之并列的前面的全局变量. 有些时候,不合时宜地插入分号,会导致严重的后果. ...

  6. jvm详情——4、分代垃圾回收详述

    虚拟机中的共划分为三个代: 年轻代(Young Generation) 年老点(Old Generation) 持久代(Permanent Generation) 其中持久代主要存放的是Java类的类 ...

  7. MHA+ProxySQL实现读写分离高可用

    最近在研究ProxySQL,觉得还挺不错的,所以就简单的折腾了一下,ProxySQL目前也是Percona在推荐的一个读写分离的中间件.关于详细的介绍可以参考官方文档.https://github.c ...

  8. leetcode — trapping-rain-water

    /** * Source : https://oj.leetcode.com/problems/trapping-rain-water/ * * Created by lverpeng on 2017 ...

  9. Gradle nexus 解决开发机器不连网无法下载gradle插件问题

    在用gradle时常规配置如下(D:\gradle-4.9\init.d\init.gradle文件,没有这个文件时自建): ext { nexus = 'http://192.168.127.128 ...

  10. MyEclipse 皮肤、主题、背景色

    第一步:打开myeclipse--->help--->install from site--->Add将路径粘贴在这里.等待安装颜色主题.https://raw.github.com ...