Python:将输入数据原样写入输出文件

时间:2017-05-11 09:47:14

标签: python-2.7 type-conversion

所以,在编程方面,我总是一个菜鸟,特别是python。我正在尝试合并两个文件(根据我的要求)并将结果存储到输出文件中。我很容易做到合并,但我面临的问题是数据格式。 您看,输出文件中的数据必须与输入文件的格式相同

这些是我的输入文件:

File1中:

ID,CLASS,BOARD_ID
3620694,Smart,233049933699
3620724,Smart,233200309044
3620819,Smart,233200971094
3620831,Smart,233201075614
3620865,Smart,233201516374
3620870,Smart,233201553354
3620906,Smart,233201863244
3620929,Smart,233201972254
3620963,Smart,233202244014
3621008,Smart,233202600234
3621107,Smart,233203534474
3621158,Smart,233204019454
3621179,Smart,233204093854
3621252,Smart,233204801364
3621254,Smart,233204815324
3621266,Smart,233205000144
3621275,Smart,233205104774
3621288,Smart,233205182584

文件2:

CDUS,CBSCS,CTRS,CTRS_ID
0,0,0.000000000375,233010056572
0,0,4.0746,233200309044
0,0,0.6182,233200971094
0,0,15.4834,233201075614
0,0,2.2459,233201516374
0,0,0.148,233201553354
0,0,0.0468,233201863244
0,0,0.5045,233201972254
0,0,0.0000000000485,233049933699

所以这是我的python脚本:

 import pandas
 #pandas.set_option('display.precision',13)
 csv1 = pandas.read_csv('OUTPUT_1707000867_BundleCrossCellData_45432477_0_0.txt',dtype={'BOARD_ID': str})
 csv2 = pandas.read_csv('I2.txt',dtype={'CTRS_ID': str}).rename(columns={'CTRS_ID':'BOARD_ID'})
 merged = pandas.merge(csv1, csv2,left_on=['BOARD_ID'],right_on=['BOARD_ID'],how='left',suffixes=('#x', '#y'), sort=True)
 merged.to_csv("Op2.txt", index=False,date_format='%Y/%m/%d %H:%M:%S.000',float_format='%.13f')

执行时收到的输出:

Op.txt

ID,CLASS,BOARD_ID,CDUS,CBSCS,CTRS 3620694,Smart,233049933699,0.0000000000000,0.0000000000000,0.0000000000485 3620724,Smart,233200309044,0.0000000000000,0.0000000000000,4.0746000000000 3620819,Smart,233200971094,0.0000000000000,0.0000000000000,0.6182000000000 3620831,Smart,233201075614,0.0000000000000,0.0000000000000,15.4834000000000 3620865,Smart,233201516374,0.0000000000000,0.0000000000000,2.2459000000000 3620870,Smart,233201553354,0.0000000000000,0.0000000000000,0.1480000000000 3620906,Smart,233201863244,0.0000000000000,0.0000000000000,0.0468000000000 3620929,Smart,233201972254,0.0000000000000,0.0000000000000,0.5045000000000 3620963,Smart,233202244014,,, 3621008,Smart,233202600234,,, 3621107,Smart,233203534474,,, 3621158,Smart,233204019454,,, 3621179,Smart,233204093854,,, 3621252,Smart,233204801364,,, 3621254,Smart,233204815324,,, 3621266,Smart,233205000144,,, 3621275,Smart,233205104774,,, 3621288,Smart,233205182584,,,

预期产出:

ID,CLASS,BOARD_ID,CDUS,CBSCS,CTRS 3620694,Smart,233049933699,0,0,0.0000000000485 3620724,Smart,233200309044,0,0,4.0746000000000 3620819,Smart,233200971094,0,0,0.6182000000000 3620831,Smart,233201075614,0,0,15.4834000000000 3620865,Smart,233201516374,0,0,2.2459000000000 3620870,Smart,233201553354,0,0,0.1480000000000 3620906,Smart,233201863244,0,0,0.0468000000000 3620929,Smart,233201972254,0,0,0.5045000000000 3620963,Smart,233202244014,,, 3621008,Smart,233202600234,,, 3621107,Smart,233203534474,,, 3621158,Smart,233204019454,,, 3621179,Smart,233204093854,,, 3621252,Smart,233204801364,,, 3621254,Smart,233204815324,,, 3621266,Smart,233205000144,,, 3621275,Smart,233205104774,,, 3621288,Smart,233205182584,,,

正如您在Op.txt中看到的,CDUS和CBSCS的值与File2.txt中的格式不同。出于显而易见的原因,两者必须采用相同的格式。

有没有办法解决这个问题?

此外,由于输入文件是在运行时生成的,因此我无法静态地将某个列类型转换为特定类型。

我还引用了以下链接,但没有找到合适的解决方案。非常感谢任何帮助。

How do I suppress scientific notation in Python? Force python to not output a float in standard form / scientific notation / exponential form Casting float to string without scientific notation supress scientific notation when writing python floats to files Suppressing scientific notation in pandas?

0 个答案:

没有答案