[英]sqlite saving text file and output as it is : python
我正在嘗試使用python將文本文件保存在sqlite中,但是當我嘗試檢索該文件時,看到換行符和其他字符。 有什么辦法可以像原始文件一樣輸出文件? 有沒有解決的辦法。
import sqlite3
conn=sqlite3.connect('example.db')
c=conn.cursor()
c.execute("create table code_tag1 (language text, code BLOB, tag text)")
#c.execute("select * from sqlite_master")
with open("bubblesort_java.txt","rb") as f:
ablob=f.read()
c.execute("insert into code_tag1 values('java',?,'bubblesort')",[buffer(ablob)])
conn.commit()
row=c.execute("select * from code_tag1").fetchone()
print (repr(str(row[1])))
conn.close()
輸出為:
'import java.util.Scanner;\r\n \r\nclass BubbleSort {\r\n public static void main(String []args)
{\r\n int n, c, d, swap;\r\n Scanner in = new Scanner(System.in);\r\n \r\n
System.out.println("Input number of integers to sort");\r\n n = in.nextInt();\r\n \r\n int
array[] = new int[n];\r\n \r\n System.out.println("Enter " + n + " integers");\r\n \r\n for
(c = 0; c < n; c++) \r\n array[c] = in.nextInt();\r\n \r\n for (c = 0; c < ( n - 1 );
c++) {\r\n for (d = 0; d < n - c - 1; d++) {\r\n if (array[d] > array[d+1]) /* For
descending order use < */\r\n {\r\n swap = array[d];\r\n array[d]
= array[d+1];\r\n array[d+1] = swap;\r\n }\r\n }\r\n }\r\n \r\n
System.out.println("Sorted list of numbers");\r\n \r\n for (c = 0; c < n; c++) \r\n
System.out.println(array[c]);\r\n }\r\n}'
如何獲取原始文件中的代碼?
謝謝
阿比納夫
輸出沒有問題。 您正在打印字符串的repr
,這就是為什么它顯示\\r
, \\n
等的原因。
>>> data = """Hello
... World"""
>>>
>>> print data
Hello
World
>>> print repr(data)
'Hello\nWorld'
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.