我对python很陌生,对ASCII文件读取:)非常陌生
当我从模拟程序导出结果时,我得到一个ASCII文件,格式为:
0,0000000E+00004,5000000E+0000
5,0000000E-0001 4,5063043E+0000
1,0000000E+00004,5117097E+0000
1,5000000E+00004,5188112E+0000
2,0000000E+00004,5230832E+0000
没有标题!每行包含两个数字(我想生成的图中的"x"参数和"y"参数(。尽管用逗号代替了小数点,并且两个参数数字没有分成列。
你能帮忙吗?
附言很抱歉没有提供代码示例,但它们都大错特错
一个更实用的解决方案:
def convert_to_float(num_with_comma):
return float(num_with_comma.replace(',', '.'))
with open('test.txt') as f:
# we strip each line of its trailing 'n' and split it
points = (line.strip().split() for line in f)
# we convert the strings to floats if the string wasn't empty
points = (map(convert_to_float, point) for point in points if point)
# and separate the x and y coords
x, y = zip(*points)
print(x, y)
# (0.0, 0.5, 1.0, 1.5, 2.0) (4.5, 4.5063043, 4.5117097, 4.5188112, 4.5230832)