简体   繁体   English

在CSV文件中编写和读取浮点数和字符串-python

[英]Writing and reading floats and strings in a CSV file - python

I am a bit new to python and programming. 我对python和编程有点陌生。 In my code, I have developed a feature (which is a 1-D array of 39 elements) for each audio file. 在我的代码中,我为每个音频文件开发了一个功能(由39个元素组成的1-D数组)。 I want to write the name of the file, the feature and its target value {0,1} into a CSV file to train my SVM classifier. 我想将文件名,功能及其目标值{0,1}写入CSV文件中,以训练我的SVM分类器。 I used the CSV writer as follows. 我使用了CSV编写器,如下所示。

with open('train.csv', 'a') as csvfile:
    albumwriter = csv.writer(csvfile, delimiter=' ')
    albumwriter.writerow(['1.03 I Want To Hold Your Hand'] + Final_feature + [0] )

I want to write the details of around 180 audio files to this CSV file and feed it to the SVM classifier. 我想将大约180个音频文件的详细信息写入此CSV文件,并将其提供给SVM分类器。 The code that I use to read the file is: 我用来读取文件的代码是:

with open('train.csv', 'rb') as csvfile:
    albumreader = csv.reader(csvfile, delimiter=' ')
    data = list()
    for row in albumreader:
        data.append(row[0:]) 
data = np.array(data)

I can access the name of the file in the first row as data[0][1] and the feature as data[0][2] but both of them are in <type 'numpy.string_'> . 我可以在第一行中以data[0][1]访问文件名,并以data[0][2]但是它们都在<type 'numpy.string_'> I want to convert the feature into a list of floats. 我想将功能转换为浮点数列表。 The main problem seems to be the ',' that separates the elements in the list. 主要问题似乎是用','分隔列表中的元素。 I tried using .astype(np.float) but in vain. 我尝试使用.astype(np.float)但徒劳。

Can anyone suggest me a good method to convert the strings from the CSV file back to the floats? 谁能建议我一个将字符串从CSV文件转换回浮点数的好方法? Your help is very much appreciated as I have very less time to complete this project. 非常感谢您的帮助,因为我很少有时间来完成此项目。 Thanks in advance. 提前致谢。

Edit: As per the comment, this is how my train.csv looks like: 编辑:根据评论,这就是我的train.csv的样子:

"1.01 I saw her standing there" "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38]" 0

"1.02 I saw her" "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]" 0

"1.03 I want to hold your hand" "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]" 1

I don't get exactly what you want to achieve, but assuming Final_feature is a python list of floats, and according to your code snippets for writing the csv file, you get the list as a string which probably looks like this: (which you get in data[0][2]) 我并没有确切地想要实现,但是假设Final_feature是一个浮点数的python列表,并根据用于编写csv文件的代码段,您可以将该列表作为一个字符串来获取,该字符串可能看起来像这样:(输入数据[0] [2])

feature = '[3.14, 2.12, 4.5]' # 3 elements only for clarity

You asked how to convert this string to float, you can use: 您询问如何将此字符串转换为float,可以使用:

map(float, feature[1:-1].split(','))

For reference, map applies its first argument to every element of its second argument, thus transforming every string in a float and returning a list of floats. 作为参考,map将其第一个参数应用于其第二个参数的每个元素,从而转换浮点数中的每个字符串并返回一个浮点数列表。

Another solution would be to write each element of your Final_feature in a separate column. 另一个解决方案是将Final_feature的每个元素写在单独的列中。

To convert string like "[1.0, 2.0, 3.0]" to list [1.0, 2.0, 3.0]: 要将“ [1.0,2.0,3.0]”之类的字符串转换为列表[1.0,2.0,3.0]:

# string to convert
s = '[1.0, 2.0, 3.0]'

lst = [float(x) for x in s[1: -1].split(',')]

# and result will be
[1.0, 2.0, 3.0]

This works both with standard python string type and with numpy.string type. 这适用于标准python字符串类型和numpy.string类型。

From what I can see, the variable Final_feature is a list of floats? 从我可以看到,变量Final_feature是一个浮点数列表? In which case based on how you wrote the file the following will import the data 在这种情况下,根据您编写文件的方式,以下内容将导入数据

with open('train.csv', 'rb') as csvfile:
    albumreader = csv.reader(csvfile, delimiter=' ')

    audio_file_names = []
    final_features = []
    target_values = []
    for row in albumreader:
        audio_file_names.append(row[0])
        final_features.append([float(s) for s in row[1:-1]])
        target_values.append([int(s) for s in row[-1]])

There are two list comprehensions to convert the data into floats and integers. 有两种列表推导可将数据转换为浮点数和整数。

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

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