繁体   English   中英

Python 导入文本文件并将字符串转换为浮点数

[英]Python importing a text file and converting string to float

我在将 txt 文件输入 python 时遇到了一些值错误。

txt 文件名为“htwt.txt”,并包含以下数据:

Ht Wt

169.6 71.2

166.8 58.2

157.1 56

181.1 64.5

158.4 53

165.6 52.4

166.7 56.8

156.5 49.2

168.1 55.6

165.3 77.8

当我输入下面的代码时,就会发生值错误。

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import os


import statsmodels.api as sm

from statsmodels.formula.api import ols


os.chdir("/Users/James/Desktop/data/")

data1=np.loadtxt("htwt.txt") 

df1=pd.DataFrame(data1)

ValueError:无法将字符串转换为浮点数:'Ht'

我可以知道正确的代码应该是什么才能将其转换为数据框吗? 谢谢。

Pandas read_csv就够了

import pandas as pd
import os
os.chdir("/Users/James/Desktop/data/")

df1 = pd.read_csv("htwt.txt",sep=' ')

Output:

>>> df1
      Ht    Wt
0  169.6  71.2
1  166.8  58.2
2  157.1  56.0
3  181.1  64.5
4  158.4  53.0
5  165.6  52.4
6  166.7  56.8
7  156.5  49.2
8  168.1  55.6
9  165.3  77.8

检查类型:

>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Ht      10 non-null     float64
 1   Wt      10 non-null     float64
dtypes: float64(2)
memory usage: 288.0 bytes

就像上面提到的 pandas read_csv工作,但如果你坚持使用np.loadtxt你可以跳过不能转换为浮点数的第一行。 你可以做:

data1 = np.loadtxt("htwt.txt", skiprows=1)

文本文件的第一行包含字母数字字符:“Ht Wt”。 这些字符不能转换为浮点数。 删除第一行,你应该没问题。

#for skipping of the first line file1 = open("hwwt.txt") lines = file1.readlines()[1:] for line in lines: print(line.rstrip()) OUTPUT #otherwise you can use read_csv which is enough

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM