天天看点

python将字符串写入txt文件_如何在python中将字符串'\ t'写入文件?(How do I write string '\t' to a file in python?)...

如何在python中将字符串'\ t'写入文件?(How do I write string '\t' to a file in python?)

我正在尝试创建一个表生成器。 但是,当我在Python中这样做时

create_query += 'ROW FORMAT DELIMITED FIELDS TERMINATED BY \'\t\'\n'

table.write(create_query)

table.write将写入文件

这就是我得到的。

ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '

我假设空格是实际的TAB

但是我想要的是这个。

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

I'm trying to create a table generator. However, when I do this in Python

create_query += 'ROW FORMAT DELIMITED FIELDS TERMINATED BY \'\t\'\n'

table.write(create_query)

table.write will write to a file

This is what I get.

ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '

I assume that blank space is the actual TAB

However what I want is this.

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

原文:https://stackoverflow.com/questions/35685809

2019-10-15 19:10

满意答案

对字符串作为一个整体使用双引号,并转义反斜杠:

create_query += "ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\t'\n"

Use double quotes for the string as a whole, and escape the backslash:

create_query += "ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\t'\n"

2016-02-28

相关问答

使用subprocess.Popen打开一个到Machine2的新SSH进程,然后将数据写入其STDIN。 import subprocess

cmd = ['ssh', '[email protected]',

'mkdir -p output/dir; cat - > output/dir/file.dat']

p = subprocess.Popen(cmd, stdin=subprocess.PIPE)

your_inmem_data = 'foobarbaz\0' * 102...

尝试BeautifulSoup html抓取: from requests import request

from bs4 import BeautifulSoup as bs

with open('C:\Users\Sadiq\Desktop\IdList.txt', 'w') as out:

for page in range(1,11):

url='http://fanpagelist.com/category/top_users/view/list/sort/f...

写入原始字节可能会导致字符编码出现问题。 正如Jon Skeet所说,使用作家...... try {

FileWriter writer = new FileWriter(data_file, true);

writer.write(str);

} catch(IOException e) {

// oh no!

} finally {

writer.close();

}

Writing raw bytes may cause problems with cha...

您可以使用str.strip从字符串的任意一边删除给定字符集的所有实例: >>> s = '''"hello!"'''.strip('!"')

>>> print(s)

hello

将output.write调用改为output.write(str(item[0]).strip('"')) ,它将删除任何不需要的引号。 You can remove all instances of a given set of characters from either side of a string wi...

我发现, read()也是一个选项,导致这样一个简单的解决方案: io = cStringIO.StringIO(logfile.read())

for line in GzipFile(fileobj=io).readlines():

impression = LogParser._parseLine(line)

if impression is not None:

impressions.append(impressio...

文本将写入工作目录中名为“Output.txt”的文件(通常是执行脚本的目录)。 要显示工作目录,您可以使用: >>> import os

>>> os.getcwd()

'/home/adam'

The text is written to a file called "Output.txt" in your working directory (which is usually the directory from which the script has been executed). T...

你需要逃避转义字符: str = 'some words\n'

with open(newfile, 'a') as the_file:

str = str.replace("\n","\\n")

the_file.write(str)

You need to escape the escape character: str = 'some words\n'

with open(newfile, 'a') as the_file:

str = str.replace("\...

Python2.7 csv模块本身无法处理unicode。 但是文档中有一个如何在类UnicodeWriter中执行此操作的示例。 你也可以试试python3,因为csv模块会在本地处理unicode。 从我链接的文档中已经无耻地撕掉了这个片段 class UnicodeWriter:

"""

A CSV writer which will write rows to CSV file "f",

which is encoded in the given encoding....

您应该在向文件写入内容后调用close方法。 lista = [i for i in range(10)]

file=open("file.txt","a")

file.write("\n")

file.write("".join(str(lista)))

file.close()

或者使用with语句自动关闭文件: lista = [i for i in range(10)]

with open("file.txt","a") as file:

...

对字符串作为一个整体使用双引号,并转义反斜杠: create_query += "ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\t'\n"

Use double quotes for the string as a whole, and escape the backslash: create_query += "ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\t'\n"

相关文章

Python 字符串操作,字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能

...

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

字符串的格式化 在python中也有类似于c中的printf()的格式输出标记。在python中格式化

...

Java 流(Stream)、文件(File)和IO Java.io包几乎包含了所有操作输入、输

...

http://halfhalf.posterous.com/dont-work-be-hated-lo

...

Data Week: Becoming a data scientist Data Pointed,

...

ImageIO.write(image, "jpeg", response.getOutputStre

...

Solr在启动时,通过多线程的方式加载core,在加载完每个core的配置文件后,实例化了一个Solr

...

B - Encoded Love-letter Time Limit:1000MS Memory

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定