亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Pandas 將 dat 文件轉換為 excel——將數字格式化為字符串

使用 Pandas 將 dat 文件轉換為 excel——將數字格式化為字符串

一只斗牛犬 2021-12-21 10:37:24
我正在嘗試將 .dat 文件轉換為 Excel(實際上我更愿意將內容復制到 Excel 文件中),但數字粘貼為文本。我希望它們是花車。我認為我遇到的問題之一是 .dat 文件是一種時髦的格式。import pandas as pddf = pd.read_csv('C:/Users/me/Desktop/specimen/spec01/specimen.dat', sep='\t')df.to_excel('C:/Users/me/Desktop/Rebar/specimen/specimen.xlsx')
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

引用的文件有一個過于華麗的標題,必須加以處理。假設您從無法更改的來源(例如服務器或實驗設備)重復接收此文件,您需要以編程方式處理以下內容:


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

必須將其刪除到一組正確的標頭中才能成功使用 Pandas 導入文件。


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

http://img1.sycdn.imooc.com//61c13e0c0001ea4603370092.jpg

我應該得到一個直接的“接受的答案”!祝你好運。


查看完整回答
反對 回復 2021-12-21
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號