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

问题描述

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

名为“htwt.txt”的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'

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

解决方法

熊猫read_csv就够了

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

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

输出:

>>> 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 $v.reset is not a function 可以工作,但是如果您坚持使用 read_csv,您可以跳过无法转换为浮点数的第一行。你可以这样做:

np.loadtxt
,

文本文件的第一行包含字母数字字符:“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