Python中将变量按行写入txt文本中

案例一:

讲数组a 循环写入名称为2.txt的文档中

# -*-coding:utf8-*-

import requests
from lxml import etree

a=[1,2,3,4,5,6]

print(a)

for i in a:
    f = open(‘C:/Users/Beckham/Desktop/python/2.txt‘,‘a‘)
    f.write(‘\n‘+str(i))
    f.close()

脚本执行结果

脚本

f = open(‘C:/Users/Beckham/Desktop/python/2.txt‘,‘a‘)

一行‘a‘,代表追加模式‘a‘,可以实现多次写入。即每次执行都会在上一行的基础上,换行写入。还有一个模式,在‘w‘写入模式下,当我们下次写入变量时,会覆盖原本txt文件的内容

如果要按行写入,我们只需要再字符串开头或结尾添加换行符‘\n‘即可,参见

f.write(‘\n‘+str(i))

案例二:

当需要写入的数据需要编码的时候,否则会提示“‘gbk‘ codec can‘t encode character ‘\xa0‘ in position 51: illegal multibyte sequence”

# -*-coding:utf8-*-
# encoding:utf-8

.....................#省略中间部分

test3=test2+test#把链接和提取码拼接成一条数据
        print(test3)
        with open(‘C:/Users/Beckham/Desktop/python/1.txt‘, ‘a‘,encoding=‘utf-8‘) as w:
            w.write(‘\n‘+test3)
            w.close()

我们就需要对打开的txt文本进行编码处理

参考:

https://blog.csdn.net/Flying_sfeng/article/details/75009741

https://blog.csdn.net/zyx_ly/article/details/89641158

原文地址:https://www.cnblogs.com/becks/p/11372283.html

时间: 2024-08-10 12:50:57

Python中将变量按行写入txt文本中的相关文章

matlab中字符串分割以及将工作区的变量写入txt文本中

一 字符串分割 matlab中最常用的字符串分割函数有两个,都比较好用,分别是strsplit和strtok. 1 strsplit 假设需要分割的字符串为str,直接使用 strsplit(str) 就可以分割,默认按空白字符分割,分割后的字符组成元胞数组. >> str = 'hello world, I am a student!' str = hello world, I am a student! >> s = strsplit(str); >> s s =

读取txt正则匹配行写入txt

StreamReader sr = new StreamReader("C:\\Users\\Administrator\\Desktop\\blogbbs\\dd.txt",Encoding.Default); StringBuilder sb = new StringBuilder(); string s = ""; string patt = "[(.+?)]"; Regex r = new Regex(patt); while((s =

将文件字节输出流写入到文本中

1 package DEMO; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.OutputStream; 7 import java.util.Scanner; 8 9 /* 10 * 文件字节输出流 2014-7-29 11 * 1.给出输出流的目的地 12 * 2.创建指向目的地的输出流 13 * 3.人输出流把数据写入到目的

【ThinkingInC++】4、统计txt文本中单词的个数

其中要使用的txt文本! header defines classes for file IO, including ifstream, whose constructor takes a file name an argument. The expression f >> word extracts the next non-whitespace token from the file and returns the stream. When a stream appears in a bo

JavaSE8基础 FileOutputStream write 写入txt文本时实现数据追加

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: txt文件初始状态 code: package jizuiku0; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* * @version V17.09

在项目中 将秒杀的时候锁定线程并将线程操作的ID写入txt文件中 (例子)

锁定线程,进行单线程操作的时候,应使用以下操作方法锁定: private readonly static Object thisLock = new Object(); lock (thisLock) { ... } //类 private void writeLog(Exception e) { FileStream fs = new FileStream("d:/ceshixiancheng.txt", FileMode.Append); //获得字节数组 byte[] data

JAVA读取TXT文本中的数据

现在在Demo.txt中存在数据: ABC 需要将ABC从文本文件中读取出来 代码片: import java.io.*; class FileReaderDemo { public static void main(String[] args) throws IOException { //创建一个文件读取流对象,和指定名称的文件相关联. //要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException FileReader fr = new FileReader

java 从txt文本中随机获取名字

代码: /* 获取随机文件文字 */ public static String random(String path) {//路径 String name = null; try { //把文本文件中的数据存储到集合中 BufferedReader reader = new BufferedReader(new FileReader(path)); //定义集合数组 ArrayList<String> list = new ArrayList<String>(); String l

mysql导入txt文本数据

按照mysql官方文档指示下 创建数据库和表: mysql> create database menagrie; 然后创建表: 1 mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), 2 species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); 然后将以下数据写入一个pet.txt文本中: Fluffy Harold cat f 1993-02-04 \NClaw