天天看點

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__中定