python转码的问题
我白天提问的问题后又出新问题了,代码如下:frombs4importBeautifulSoupimportosimportcodecsWORK_PATH=os.path....
我白天提问的问题后又出新问题了,代码如下:
from bs4 import BeautifulSoup
import os
import codecs
WORK_PATH = os.path.dirname(__file__)
print WORK_PATH
print os.getcwd()
print os.path.abspath('.')
FILE_PATH = os.path.join(WORK_PATH,'ert.txt')
print FILE_PATH
INPUT_PATH = os.path.join(WORK_PATH,'src.txt')
src_fl = open(INPUT_PATH,'r')
l = src_fl.read()
result_fl = open(FILE_PATH,'w')
# k = result_fl.write(l)
soup= BeautifulSoup(l)
trs=soup.findAll("string")
length=len(trs)
arr=[]
for i in range(length):
print trs[i].contents
arr.append(trs[i].contents)
print arr
for j in arr:
print j
result_fl.write(j)
result_fl.write('\n')
result_fl.close()
输出结果总是说我“result_fl.write(j)”有问题,
[u'Default']
[u'\xb2\xe2\xca\xd4\xb3\xa1\xbe\xb0:\xb6\xa9\xb2\xcd\xcc\xe1\xbd\xbb\xd2\xb3\xc3\xe6']
[u'Loop in : 2 durTime:0.266s']
[[u'Default'], [u'\xb2\xe2\xca\xd4\xb3\xa1\xbe\xb0:\xb6\xa9\xb2\xcd\xcc\xe1\xbd\xbb\xd2\xb3\xc3\xe6'], [u'Loop in : 2 durTime:0.266s']]
[u'Default']
Traceback (most recent call last):
File "C:\Users\Boxer\Desktop\1.py", line 47, in <module>
result_fl.write(j)
TypeError: argument 1 must be string or read-only character buffer, not list
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Boxer\Desktop\1.py"]
我的“src.txt”文件中的内容是:
<dict>
<key>LogType</key>
<string>Default</string>
<key>Message</key>
<string>测试场景:订餐提交页面</string>
<string>Loop in : 2 durTime:0.266s</string>
<key>Timestamp</key>
<date>2014-06-06T12:16:24Z</date>
<key>Type</key>
<integer>1</integer>
</dict>
急:
我想得到的结果是:在文本”ert.txt“中:
Default
测试场景:订餐提交页面
Loop in : 2 durTime:0.266s 展开
from bs4 import BeautifulSoup
import os
import codecs
WORK_PATH = os.path.dirname(__file__)
print WORK_PATH
print os.getcwd()
print os.path.abspath('.')
FILE_PATH = os.path.join(WORK_PATH,'ert.txt')
print FILE_PATH
INPUT_PATH = os.path.join(WORK_PATH,'src.txt')
src_fl = open(INPUT_PATH,'r')
l = src_fl.read()
result_fl = open(FILE_PATH,'w')
# k = result_fl.write(l)
soup= BeautifulSoup(l)
trs=soup.findAll("string")
length=len(trs)
arr=[]
for i in range(length):
print trs[i].contents
arr.append(trs[i].contents)
print arr
for j in arr:
print j
result_fl.write(j)
result_fl.write('\n')
result_fl.close()
输出结果总是说我“result_fl.write(j)”有问题,
[u'Default']
[u'\xb2\xe2\xca\xd4\xb3\xa1\xbe\xb0:\xb6\xa9\xb2\xcd\xcc\xe1\xbd\xbb\xd2\xb3\xc3\xe6']
[u'Loop in : 2 durTime:0.266s']
[[u'Default'], [u'\xb2\xe2\xca\xd4\xb3\xa1\xbe\xb0:\xb6\xa9\xb2\xcd\xcc\xe1\xbd\xbb\xd2\xb3\xc3\xe6'], [u'Loop in : 2 durTime:0.266s']]
[u'Default']
Traceback (most recent call last):
File "C:\Users\Boxer\Desktop\1.py", line 47, in <module>
result_fl.write(j)
TypeError: argument 1 must be string or read-only character buffer, not list
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\Boxer\Desktop\1.py"]
我的“src.txt”文件中的内容是:
<dict>
<key>LogType</key>
<string>Default</string>
<key>Message</key>
<string>测试场景:订餐提交页面</string>
<string>Loop in : 2 durTime:0.266s</string>
<key>Timestamp</key>
<date>2014-06-06T12:16:24Z</date>
<key>Type</key>
<integer>1</integer>
</dict>
急:
我想得到的结果是:在文本”ert.txt“中:
Default
测试场景:订餐提交页面
Loop in : 2 durTime:0.266s 展开
1个回答
展开全部
你好:
修改后的代码如下:
from bs4 import BeautifulSoup
import os
import codecs
WORK_PATH = os.path.dirname(__file__)
print WORK_PATH
print os.getcwd()
print os.path.abspath('.')
FILE_PATH = os.path.join(WORK_PATH,'ert.txt')
print FILE_PATH
INPUT_PATH = os.path.join(WORK_PATH,'src.txt')
src_fl = open(INPUT_PATH,'r')
l = src_fl.read()
result_fl = open(FILE_PATH,'w')
# k = result_fl.write(l)
soup= BeautifulSoup(l,fromEncoding="gb18030")
trs=soup.findAll("string")
length=len(trs)
arr=[]
for i in range(length):
print trs[i].decode_contents()
arr.append(trs[i].decode_contents())
print arr
for j in arr:
print j
result_fl.write(j)
result_fl.write('\n')
result_fl.close()
更多追问追答
追问
你好,为什么我输出来的结果只有
Default这一个?
我打印出来arr是没有问题的,里面元素是全的,但是遍历arr的时候就会出问题...
追答
没明白你说的什么意思!
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询