从python写入数据返回乱码

时间:2019-02-21 18:05:32

标签: python-3.x

我正在尝试读取一个文本文件文件夹,并将它们组合为一个(宽)文件(以逗号或制表符分隔)。我的问题是换行符(或回车符)以某种奇怪的格式显示。

输入数据示例:(我已经包含了行尾字符)

Directional Deformation (in)[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]
0.[CR][LF]

合并多个输入数据时的输出:

Directional Deformation (in)L䓾椀爀攀挀琀椀漀渀愀氀 䐀攀昀漀爀洀愀琀椀漀渀 ⠀椀渀⤀ⰀDirectional Deformation (in)ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]
0.,0.,0.ബ਍ⰀⰀⰀ഍[LF]

我一直在努力寻找编码和读取/写入设置的正确组合,但到目前为止还算不上运气。下面的示例代码:

import glob
import os

# Determine the number of row in data file
tmpFiles = [open(f) for f in glob.glob("*.csv")]
nRows = sum(not line.isspace() for line in tmpFiles[0])

# fetch the file names
files = [open(f, 'r') for f in glob.glob("*.csv")]

# Combine data file columns
fout = open ("results.dat", 'w')
for row in range(1, nRows):
    for f in files:
        fout.write(f.readline().strip())
        fout.write(',')
    fout.write(os.linesep) # Not sure if this should be '\n' or ???
fout.close()

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

好的...我正在工作。在二进制读取和写入模式下使用编解码器,将以正确的格式(无乱码)创建输出文件。请注意,它也被编码为utf-16(尽管python报告了cp1252,我认为这是乱码的输出)。

输入文件是在ANSYS解决方案结果中用(右键单击)“导出文本文件”编写的ANSYS结果输出文件。合并单个结果文件的代码如下所示:

import os
import glob
import codecs

# Path and file settings
path = 'C:\\temp\\'

# Determine the number of row in data file
# [must request in binary mode for correct count]
tmpFiles = [open(f, 'rb') for f in glob.glob("*.csv")]
nRows = sum(not line.isspace() for line in tmpFiles[0])

# Read individual results files 
files = [codecs.open(f, 'rb', encoding='utf-16') for f in glob.glob("*.csv")]
with codecs.open(path + 'results.dat', 'wb', encoding='utf-16') as fout:
    for row in range(1, nRows):
        for f in files: 
            fout.write(f.readline().strip())
            fout.write(',')
        fout.write(os.linesep)
fout.close()

现在temp文件夹中的三个结果文件的输出文件如下所示:(我需要删除最后一个逗号...)

Directional Deformation (in),Directional Deformation (in),Directional Deformation (in),
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,
0.,0.,0.,

感谢您的帮助...问候