简体   繁体   English

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

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

I have encountered some value error when input txt file into python.我在将 txt 文件输入 python 时遇到了一些值错误。

the txt file called "htwt.txt", and contain the below data: 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

When I typed the below code, and value errors are occurred.当我输入下面的代码时,就会发生值错误。

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: could not convert string to float: 'Ht' ValueError:无法将字符串转换为浮点数:'Ht'

May I know what should the correct code so that it can be converted to the data frame?我可以知道正确的代码应该是什么才能将其转换为数据框吗? thanks.谢谢。

Pandas read_csv is enough Pandas read_csv就够了

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

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

Output: 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

Checking the types:检查类型:

>>> 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

Like mentioned above pandas read_csv works, but if you insist on using np.loadtxt you can skip the first row which can't be converted to a float.就像上面提到的 pandas read_csv工作,但如果你坚持使用np.loadtxt你可以跳过不能转换为浮点数的第一行。 You can do:你可以做:

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

The first row in your text file has alphanumeric characters: "Ht Wt".文本文件的第一行包含字母数字字符:“Ht Wt”。 These characters cannot be converted to a floating point number.这些字符不能转换为浮点数。 Remove the first row and you should be fine.删除第一行,你应该没问题。

#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