我正在尝试将.dat文件转换为Excel(实际上,我希望将其内容复制到Excel文件中),但是数字会粘贴为文本。我希望他们是浮游生物。
我认为我遇到的问题之一是.dat文件采用时髦的格式。
import pandas as pd
df = pd.read_csv('C:/Users/me/Desktop/specimen/spec01/specimen.dat', sep='\t')
df.to_excel('C:/Users/me/Desktop/Rebar/specimen/specimen.xlsx')
答案 0 :(得分:0)
被引用文件的头文件过于华丽,必须对其进行处理。假设您从服务器或实验设备等无法更改的源反复接收到此文件,则可以通过编程方式处理以下内容:
MTS793|MPT|ENU|1|2|.|/|:|1|0|0|A
Operator Information Time: 15.133789 Sec 8/2/2018 12:18:30 PM
Is the extensometer pin pulled? ughiopuh;u
Operator Information End
Data Acquisition Tensile Test Data Time: 117.81934 Sec 8/2/2018 12:20:13 PM
Time Axial Displacement Axial Strain Axial Force
Sec in in/in lbf
必须将其删除为适当的标头集,才能成功使用熊猫导入文件。
Time;Axial Displacement;Axial Strain;Axial Force
Sec in;in/in;lbf
可能是所需的标题。这将带您进入不断发展的列多索引世界。
这是一个有效的答案:
import openpyxl
import pandas as pd
import sys
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
csvdata = StringIO("""MTS793|MPT|ENU|1|2|.|/|:|1|0|0|A
Operator Information Time: 15.133789 Sec 8/2/2018 12:18:30 PM
Is the extensometer pin pulled? ughiopuh;u
Operator Information End
Data Acquisition Tensile Test Data Time: 117.81934 Sec 8/2/2018 12:20:13 PM
Time Axial Displacement Axial Strain Axial Force
Sec in in/in lbf
15.372559 -0.00026854035 -0.00013428145 -3.7030973
15.472656 2.2532094e-08 -8.3925901e-05 11.109222
15.572754 -0.00026854035 -0.00011749626 3.7030623
15.672852 2.2532094e-08 -0.00011749626 -1.7583034e-05
15.772949 0.00026858543 -0.00010071108 7.4061422""")
df = pd.read_csv(csvdata, sep="\t", header=[5,6])
# Show how pandas parses the file headers
print(df.head(6))
# The columns are referenceable as a multiindex
level0 = df.columns.get_level_values(0)
level1 = df.columns.get_level_values(1)
# Set index from multiindex columns!
# But we are not going to assign this result, because
# there is a better trick
print(df.set_index(('Time', 'Sec')))
flattened_cols = (["{} ({})".format(x,y) for x,y in zip(level0, level1)])
df.columns = flattened_cols
# The better trick
df.set_index(flattened_cols[0], inplace=True)
# show results
print(df.head(6))
# save in desired file format
df.to_excel('specimen.xlsx')
结果存入内存
Axial Displacement (in) Axial Strain (in/in) Axial Force (lbf)
Time (Sec)
15.372559 -2.685404e-04 -0.000134 -3.703097
15.472656 2.253209e-08 -0.000084 11.109222
15.572754 -2.685404e-04 -0.000117 3.703062
15.672852 2.253209e-08 -0.000117 -0.000018
15.772949 2.685854e-04 -0.000101 7.406142
xls中的结果
我应该为此提供一个直截了当的“接受的答案”!祝你好运。