楼主: wolfccb
2986 1

[程序分享] Stata导出中文乱码的解决方案 [推广有奖]

  • 0关注
  • 0粉丝

大专生

5%

还不是VIP/贵宾

-

威望
0
论坛币
266 个
通用积分
143.0955
学术水平
8 点
热心指数
10 点
信用等级
8 点
经验
661 点
帖子
10
精华
0
在线时间
69 小时
注册时间
2011-9-19
最后登录
2024-8-12

楼主
wolfccb 发表于 2023-4-18 15:44:55 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Stata有一些导出统计、回归结果的工具,比如outreg2, asdoc, esttab,但这几个工具对中文的支持都不好,导出rtf文件里的中文会变成乱码。这是由于它们用了UTF-8编码导出,rtf的标准不认得它。

我折腾了一下,写了个小工具把rtf文件的UTF-8转成Word能够认识的Unicode编码。用法很简单,把这个脚本保存为一个.vbs文件,然后把Stata导出的rtf文件拖到这个vbs文件的图标上,它就会生成一个加了后缀“_c”的rtf文件,乱码消失不见。理论上这个小工具也能解决日文、韩文等其它多字节编码问题。

这段代码是匆匆写就的,效率低且丑,欢迎修改。

'Purpose:   Encode rtf file containing Multi-byte-char into Unicode format, which is readable in Word.
'   A new rtf file with surfix _c will be generated in the same directory of the original file.
'Usage:     Drag and drop your RTF file onto this script. Windows XP or above is required.
'Contact:   wolfccb.com

Set objArgs = WScript.Arguments

if objArgs.Count=0 then
    msgbox "Please drag and drop your RTF file onto this script."
    WScript.Quit
end if

filename=objArgs (0)

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, 1, false)
txt = f.ReadAll()
f.Close()
txt = Multi_Encode(txt)
txt = convert(txt)

fileout = replace (filename,".rtf","_c.rtf")
Set f = fso.OpenTextFile(fileout, 2, true)
f.Write txt
f.Close()
Set f=Nothing
Set fso=Nothing
Set objArgs=Nothing

Function convert(s)
        For i = 1 To Len(s)
                if mid(s,i,2)="%E" then
                        t0=mid(s,i,9)
                        t1=DecodeUTF8(t0)
                        s=replace (s,t0,t1)
                end if
        Next
        convert=s
End Function

Function Multi_Encode(ByVal str)
        Dim i
        Dim code
        For i = 1 To Len(str)
                code = Mid(str, i, 1)
                If Asc(code) < 0 Then
                        code = Hex(Asc(code))
                        If Len(code) = 1 Then
                                code = "0" & code
                        End If
                        If CByte("&H" & Right(code, 2)) < 127 Then
                                code = "%" & Left(code, 2) & Chr(CByte("&H" & Right(code, 2)))
                        Else
                                code = "%" & Left(code, 2) & "%" & Right(code, 2)
                        End If
                End If
                Multi_Encode = Multi_Encode & code
        Next
        Multi_Encode=replace(Multi_Encode,"%5C","\")
End Function

Function DecodeUTF8(sInput)
        Dim oStream, i, b1, b2, b3
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Type = 2 'binary mode
        oStream.Open
        For i = 1 To Len(sInput) Step 3
                b1 = CByte("&H" & Mid(sInput, i + 1, 2))
                If b1 < &H80 Then 'single byte character
                          oStream.WriteText Chr(b1)
                ElseIf (b1 And &HE0) = &HC0 Then 'two byte character
                          b2 = CByte("&H" & Mid(sInput, i + 4, 2))
                          oStream.WriteText Chr(((b1 And &H1F) * &H40) + (b2 And &H3F))
                          i = i + 3
                ElseIf (b1 And &HF0) = &HE0 Then 'three byte character
                          b2 = CByte("&H" & Mid(sInput, i + 4, 2))
                          b3 = CByte("&H" & Mid(sInput, i + 7, 2))
                          oStream.WriteText ChrW(((b1 And &HF) * &H1000) + ((b2 And &H3F) * &H40) + (b3 And &H3F))
                          i = i + 6
                End If
        Next
        oStream.Position = 0 'reset stream position
        DecodeUTF8 = ""
        While Not oStream.EOS
                DecodeUTF8 = DecodeUTF8 & "\u" & CStr(AscW(oStream.ReadText(1))) & "?"
          Wend
          oStream.Close
        Set oStream = Nothing
End Function

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Stata 中文乱码 解决方案 tata Character
相关内容:stata方案解决

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 5 + 3 鼓励积极发帖讨论

总评分: 学术水平 + 3  热心指数 + 5  信用等级 + 3   查看全部评分

沙发
songking 发表于 2023-5-6 08:50:53
用putdocx可以完美解决乱码问题

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-2-8 07:45