两个unicode字符串的串联? [英] concatenation of two unicode srings?

查看:78
本文介绍了两个unicode字符串的串联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #!/ usr / bin / python 
#-*-编码:utf-8-*-

import re
分隔符= [u。,u,,u。]
dat = open(r C:\Users\User\Desktop\text4.txt,'r') .read()
text = dat.decode( utf-8)
wros = text.split()
out =
导入字符串
空格=
counter = 0;
中的单词:
out = u .join(word)

writ = open( C:\\Users\\User\ \Desktop\\text5.txt,'w')
writ.write(out.encode('utf-8'))
writ.close()

text4.txt包含



text5.txt输出为ह。



所需的输出是

请告诉我我在做什么错了?需要帮助!预先感谢

解决方案

我不知道您与 word ,但我会这样:

  text = open('text4.txt')。read()

text = text.decode( utf-8)

#将一个字符串分成(旧)单词列表
单词= text.split()

#新单词列表
out = []

#修改单词
替换单词:
#此处-使用`word`
out.append(word)

#将所有新单词连接到一个字符串
result = u''.join(out)

result = result.encode('utf-8')

writ = open('text5.txt','w')
writ.write(result)
writ .close()


#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
separators = [u"।", u",", u"."]
dat=open(r"C:\Users\User\Desktop\text4.txt",'r').read()
text=dat.decode("utf-8")
wros=text.split()
out=""
import string
space=" "
counter=0;
for word in wros:
        out=u" ".join(word)

writ=open("C:\\Users\\User\\Desktop\\text5.txt",'w')
writ.write(out.encode('utf-8'))
writ.close()

text4.txt contains भारत का इतिहास काफी समृद्ध एवं विस्तृत है।

text5.txt outputs as ह ै ।

desired output is भारत का इतिहास काफी समृद्ध एवं विस्तृत है।

please tell me what i am doing is wrong ? HElp required ! thanks in advance

解决方案

I don't know what you have to do with word but I would do it this way:

text = open('text4.txt').read()

text = text.decode("utf-8")

# split one string into list of (old) words
words = text.split()

# list for new words
out = []

# modify words
for word in words:
    # here - do something with `word`
    out.append(word)

# concatenate all new words to one string 
result = u' '.join(out)

result = result.encode('utf-8')

writ = open('text5.txt', 'w')
writ.write(result)
writ.close()

这篇关于两个unicode字符串的串联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆