1. 程式人生 > >python 讀寫檔案編碼處理問題

python 讀寫檔案編碼處理問題

字串中有不能編碼的字元
#coding:utf-8
string1=u"凝固粉末冶金技術"
print string1
f = open('data.txt','w')
f.write(string1.encode(encoding='gbk', errors='ignore'))
f.close()
#errors = 'ignore' 當編碼轉換出錯時忽略錯誤

f = open('data.txt','r')
for line in f.readlines():
    line.decode('gbk',errors='ignore')
    print line
f.close()

相關推薦

python 檔案編碼處理問題

字串中有不能編碼的字元#coding:utf-8 string1=u"凝固粉末冶金技術" print string1 f = open('data.txt','w') f.write(string1.encode(e

[Python]檔案處理log

#Filename: using_file.py poem = '''\ Programming is fun When the work is done if you wanna make your work also fun:     use Python! ''' e

python不同編碼txt檔案

以後整理規範 import os import codecs filenames=os.listdir(os.getcwd()) out=file("name.txt","w") for filename in filenames: out.write(filename

python:檔案模式

  模式 描述 r 只讀,指標在檔案開頭 w 只寫。檔案存在被覆蓋,檔案不存在,建立新檔案 a 追加。檔案存在,指標在檔案末尾寫入;檔案不存在,

Python 檔案 中文亂碼 錯誤TypeError: write() argument must be str, not bytes+

今天使用Python向檔案中寫入中文亂碼,程式碼如下: fo = open("temp.txt", "w+") str = '中文' fo.write(str) fo.close() 1 2 3 4

Python進階(二十五)-Python檔案

分享一下我的偶像大神的人工智慧教程!http://blog.csdn.net/jiangjunshow 也歡迎轉載我的文章,轉載請註明出處 https://blog.csdn.net/mm2zzyzzp Python進階(二十五)-Python讀寫檔案

python檔案模組

一 往一個檔案寫資料 1 程式碼CodeInFile.py # -*- coding:utf-8 -*- class CodeInFile(): def __init__(self,name,code,database): self.rootd

Python檔案的幾種不同方式,特別對出現‘b’的解讀

一、Python檔案讀寫的幾種模式: 'r':預設值,表示從檔案讀取資料。 'w':表示要向檔案寫入資料,並截斷以前的內容 'a':表示要向檔案寫入資料,新增到當前內容尾部 'r+':表示對檔案進行可讀寫操作(刪除以前的所有資料) 'r+a':表示對檔案可進行讀寫操作(新增到當前檔案尾部)

Python 檔案

– Start 例子1 try: file_name = r'D:\0_Shangbo\Dev\Python\python_work\test.py'; f = open(file_name) # 以只讀方式開啟檔案 content = f.read()

python——檔案

檔案有兩個關鍵的熟悉——檔名、路徑 有幾個需要注意的就是: 1.雖然資料夾名稱和檔名在Windows 和OS X 上是不區分大小寫的,但在Linux 上是區分大小寫的 2.倒斜槓‘\’和正斜槓‘/’  要能夠分清楚(windows使用前者  OS X 以及Linux使用

python檔案固定程式碼格式

import os path=“D:/a/b/c/d/test.txt” file_path,file_name=os.path.split(path) if not os.path.exists(file_path): os.makedirs(file_pat

python 檔案時,r、rt、rb、r+的區別

文字檔案中的 回車 在不同作業系統中所用的字元表示有所不同。 Windows: \r\n Linux/Unix: \n Mac OS: \r python讀寫檔案 open()中 r rb rt rt模式下

python技巧實用篇】python檔案、jieba自定義字典

import jieba from astropy.table.np_utils import join import os import sys import jieba.posseg as p

Python檔案小結

read()、readline()、readlines()的比較 read 特點是:讀取整個檔案,將檔案內容放到一個字串變數中。 劣勢是:如果檔案非常大,尤其是大於記憶體時,無法使用read()方法。 read()的返回值是字串,讀取的是整個檔案,包含檔案中的換行符。 readl

Python檔案 中文正則匹配

讀寫檔案 中文正則匹配  好麻煩 程式碼很短 錯誤調了很多 #!/usr/bin/env python #encoding: utf-8 import re f=open('10000.txt','r', encoding='UTF-8') f1=open('result.txt','w') for li

python檔案中文問題

file = open(fromFilePath,encoding='UTF-8') resultFile = open(toFilePath,'w+',encoding='UTF-8') try: text = file.readl

python檔案位元組,二進位制,分行,追加等)

http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html Python讀寫檔案 1.open 使用open開啟檔案後一定要記得呼叫檔案物件的close()方法。比如可以用try/finally語句

python檔案file寫入到mysql

cat UserGoldConsumitemDaily.py #!/usr/bin/env python #-*-coding:utf-8-*- #使用者消費物品日報 #****************************************使用說明***

Python檔案

1.open 使用open開啟檔案後一定要記得呼叫檔案物件的close()方法。比如可以用try/finally語句來確保最後能關閉檔案。 file_object = open('thefile.txt')try:     all_the_text = file_object.read( )finally:

python 檔案 把爬取的圖片資訊寫入檔案

1.開啟檔案時,file 和io.open的區別 file :如果檔案不存在直接報錯 open :如果檔案不存在先建立檔案再進行寫入 2.把字串寫入csv檔案 ab+ 追加內容到csv檔案 ab 追加內容到csv檔案但是第一行會空出來