'pd.read_fwf behave different in python 3.9.7 compared to python 3.5.2
I am a python rookie that used python code developed by a previous coworker. I have this file (life_time.txt) that needs to be read by the python script:
1_3 1_4 1_10 1_5 1_6 2_3 2_8 3_4 3_9 4_10 5_6 5_9 5_8 6_10 7_8 8_9 9_10
7066.30 4951.00 204664.31 2027.60 14498.40 41686.40 1077.00 21773.37 13321.70 284136.73 9401.60 5247.40 5802.00 67727.50 783031.81 24.00 66.00
1768.93 2421.83 84863.77 70.04 2571.77 11872.19 2.11 3917.00 1692.92 87771.22 2227.40 914.76 1104.45 23971.41 61028.71 0.00 0.00
This piece of a python script (running V.3.5.2) does the job correctly (I added the prints to try to understand the problem):
filepath_t = path + '/life_time.txt'
t_raw = pd.read_fwf(filepath_t, header=0).values
print('Test')
print(pd.__version__)
print(t_raw)
t = (t_raw[0, :]).tolist()
print(t)
t_std = (t_raw[1, :]).tolist()
print(t_std)
This is the output:
Test
0.18.1
[[ 7.06630000e+03 4.95100000e+03 2.04664310e+05 2.02760000e+03
1.44984000e+04 4.16864000e+04 1.07700000e+03 2.17733700e+04
1.33217000e+04 2.84136730e+05 9.40160000e+03 5.24740000e+03
5.80200000e+03 6.77275000e+04 7.83031810e+05 2.40000000e+01
6.60000000e+01]
[ 1.76893000e+03 2.42183000e+03 8.48637700e+04 7.00400000e+01
2.57177000e+03 1.18721900e+04 2.11000000e+00 3.91700000e+03
1.69292000e+03 8.77712200e+04 2.22740000e+03 9.14760000e+02
1.10445000e+03 2.39714100e+04 6.10287100e+04 0.00000000e+00
0.00000000e+00]]
[7066.3, 4951.0, 204664.31, 2027.6, 14498.4, 41686.4, 1077.0, 21773.37, 13321.7, 284136.73, 9401.6, 5247.4, 5802.0, 67727.5, 783031.81, 24.0, 66.0]
[1768.93, 2421.83, 84863.77, 70.04, 2571.77, 11872.19, 2.11, 3917.0, 1692.92, 87771.22, 2227.4, 914.76, 1104.45, 23971.41, 61028.71, 0.0, 0.0]
But running the same script with V. 3.9.7 produce different result (not the output what I want):
Test
1.3.4
[[1.768930e+03 2.421830e+03 8.486377e+04 7.004000e+01 2.571770e+03
1.187219e+04 2.110000e+00 3.917000e+03 1.692920e+03 8.777122e+04
2.227400e+03 9.147600e+02 1.104450e+03 2.397141e+04 6.102871e+04
0.000000e+00 0.000000e+00]]
[1768.93, 2421.83, 84863.77, 70.04, 2571.77, 11872.19, 2.11, 3917.0, 1692.92, 87771.22, 2227.4, 914.76, 1104.45, 23971.41, 61028.71, 0.0, 0.0]
If in the code I changed header=0 to header=1, python 3.9.7 works producing the output as intended:
Test
1.3.4
[[7.0663000e+03 4.9510000e+03 2.0466431e+05 2.0276000e+03 1.4498400e+04
4.1686400e+04 1.0770000e+03 2.1773370e+04 1.3321700e+04 2.8413673e+05
9.4016000e+03 5.2474000e+03 5.8020000e+03 6.7727500e+04 7.8303181e+05
2.4000000e+01 6.6000000e+01]
[1.7689300e+03 2.4218300e+03 8.4863770e+04 7.0040000e+01 2.5717700e+03
1.1872190e+04 2.1100000e+00 3.9170000e+03 1.6929200e+03 8.7771220e+04
2.2274000e+03 9.1476000e+02 1.1044500e+03 2.3971410e+04 6.1028710e+04
0.0000000e+00 0.0000000e+00]]
[7066.3, 4951.0, 204664.31, 2027.6, 14498.4, 41686.4, 1077.0, 21773.37, 13321.7, 284136.73, 9401.6, 5247.4, 5802.0, 67727.5, 783031.81, 24.0, 66.0]
[1768.93, 2421.83, 84863.77, 70.04, 2571.77, 11872.19, 2.11, 3917.0, 1692.92, 87771.22, 2227.4, 914.76, 1104.45, 23971.41, 61028.71, 0.0, 0.0]
but python 3.5.2 gets it wrong. what modification can be made to the code to make it work for both versions? Thanks for any feedback
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
