简体   繁体   English

如何将字符串写入CSV文件

[英]How to write strings into a csv file

I am trying to create a csv file with a single column of file paths. 我试图用文件路径的单列创建一个csv文件。 I need a second column filled with ones. 我需要填充第二列。

The result I want to get is as follows; 我想要得到的结果如下:

./1/a_1.csv, 1
./1/a_2.csv, 1
./1/a_3.csv, 1

The code I tried is this; 我试过的代码是这样的;

import numpy as np

data=np.chararray((650,2), itemsize=20)

for i in range(1, 650):
    data[0][0]="./1/a_" + str(i) + ".csv"

np.savetxt("C:\\a.csv", data, delimiter = ",")

However, it doesn't seem to work. 但是,它似乎不起作用。 Could you help? 你能帮忙吗?

You don't need Numpy for this. 您不需要Numpy。 Just do something like 只是做类似的事情

with open('a.csv', 'w') as outf:
    for i in range(650):
        print('./1/a_%s.csv, 1' % (i + 1), file=outf)

and you're golden. 而且你很黄金。

If you don't care about how the file is created (as long as it works), go for AKX's solution. 如果您不关心文件的创建方式(只要它可以工作),请寻求AKX的解决方案。

If you want to do it with Numpy, you need to get your dtype straight. 如果要使用Numpy进行此操作,则需要使dtype变直。 np.savetxt has a default formatter which has trouble understanding the array you gave it. np.savetxt具有默认的格式化程序,该格式化程序无法理解您提供给它的数组。

This is how I managed to get your desired output: 这就是我设法获得所需输出的方式:

data = np.empty((650,2),dtype="S20") # empty array of the correct datatype 

data[:,1] = '1' # set column 1 to '1' (as you noted)

for i in range(650):
    data[i][0] = "./1/a_" + str(i) + ".csv" # put your filenames into first column

np.savetxt(r"C:\a.csv", data, delimiter = ",", fmt="%s") # save it using %s formatter

In each iteration of your loop, you only ever write to data[0][0] . 在循环的每次迭代中,您只能写入data[0][0] You need to alter your loop to change the array position you're setting as you loop over the range. 在循环范围内时,您需要更改循环以更改要设置的阵列位置。

In csv file the delimiter is ";" 在csv文件中,定界符为“;” not ",". 不是“,”。

import numpy as np

data=np.chararray((650,2), itemsize=20)

for i in range(1, 650):
    data[0][0]="./1/a_" + str(i) + ".csv"

np.savetxt("C:\\a.csv", data, delimiter = ";")

I would use Dataframes from the Pandas library. 我将使用Pandas库中的数据框。 A similar approach to chitown88's answer, but more compact. 与chitown88的答案类似的方法,但更为紧凑。

import pandas as pd

# create a list with paths to files named from 1 to 256
file_paths = ["./1/a_{}.csv".format(i) for i in range(1,257)]

# add the list to a Pandas Dataframe, the column name can be omitted
df = pd.DataFrame(data=file_paths, columns=['Paths'])

# add a column of 1's to the DataFrame
df['1'] = 1

# write the DataFrame without the indices and column names to a csv file using a specific separation character  
df.to_csv('C:\\a.csv', sep=',', index=False, header=False)

I personally like to store all my data into a dataframe and then at he very end, save it to a csv file, as opposed to writing after each iteration. 我个人喜欢将所有数据存储到一个数据帧中,然后在最后,将其保存到一个csv文件中,而不是在每次迭代后进行写操作。

so I initialize a blank dataframe called results 所以我初始化了一个空白的数据框,称为results

then iterates through your range, creating your file_path string. 然后遍历您的范围,创建您的file_path字符串。 Then I create that 1 row with the current file_name string and your 2nd column. 然后,使用当前的file_name字符串和第二列创建第一行。 That gets appended to the results dataframe, then moves on to the next item in your list/range. 该数据将附加到结果数据框中,然后移至列表/范围中的下一项。 Continues that process until it's done, then saves the final results dataframe to file 继续该过程,直到完成为止,然后将最终结果数据帧保存到文件中

import pandas as pd

# initialize an empty dataframe 
results = pd.DataFrame()

# iterates each element in the range list to form a row that is appended to the results dataframe
for i in range(1, 650):
    file_path="./1/a_" + str(i) + ".csv"
    temp_df = pd.DataFrame([[file_path, 1]], columns = ['file_path', 'col_2'])
    results = results.append(temp_df.reset_index(drop = True))

# after iterating through all the elements, saves completed results dataframe to file
results.to_csv("a.csv", index=False))

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

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