有没有办法将 txt 列写入 csv 文件?
Is there a way of writing txt columns to a csv file?
所以我有 8 个不同的 .txt 文件,我想从中提取 1 个数据列并创建一个包含所有这些数据列的 csv 文件。我用它来获取我想要的列:
china_data = pd.read_csv('China results.txt', header=None, usecols=[0], sep='\t')
但我找不到任何地方解释如何将这些数据列添加到单个 csv 文件夹。
我看过
f.open("filename", "w")
但不确定我是否可以将其用于我正在尝试做的事情。
编辑:我正在合并的文件采用这种格式,
0
0 1.0
1 1.0
2 1.0
3 1.0
4 1.0
... ...
14897 1.0
14898 1.0
14899 1.0
14900 1.0
14901 1.0
[14902 rows x 1 columns]
据我了解,您需要读取 txt
个文件并将第一列写入 csv
个文件。对于您的用例,我编写了函数来读取 csv、提取第一列、组合数据的所有第一列并将它们写入最后的 csv
文件。为此,您需要指定数据文件的文件名。
def read_csv(file_path):
with open(file_path, "r") as f:
lines = f.readlines()
return lines[1:len(lines)-1] #ignoring your first line and last line
def extract_first_column(file_path): #returns first column
lines = read_csv(file_path)
first_col = []
for elem in lines:
elem = elem.strip().split(" ")
if not elem == "":
first_col.append(elem[0])
return first_col
def combined_array(file_path_list): #combines all of columns to one array
all_values = []
for file_path in file_path_list:
col_data = extract_first_column(file_path)
all_values.append(col_data)
return all_values
def write_csv(array, csv_name, delim): #writes the array to a csv file
with open(csv_name,"w") as f:
for elem in array:
if not elem == "":
f.write(elem + delim)
file_name_list = ["data.txt"] #you specify all 7 filenames here
array = combined_array(file_name_list) #array containing all 7 columns
array = [x for sublist in array for x in sublist]
write_csv(array, "test.csv", "\n") #writing to csv file
你提供的示例文件对我有用。如果我误解了你的要求,我会更新我的答案。
所以我有 8 个不同的 .txt 文件,我想从中提取 1 个数据列并创建一个包含所有这些数据列的 csv 文件。我用它来获取我想要的列:
china_data = pd.read_csv('China results.txt', header=None, usecols=[0], sep='\t')
但我找不到任何地方解释如何将这些数据列添加到单个 csv 文件夹。
我看过
f.open("filename", "w")
但不确定我是否可以将其用于我正在尝试做的事情。
编辑:我正在合并的文件采用这种格式,
0
0 1.0
1 1.0
2 1.0
3 1.0
4 1.0
... ...
14897 1.0
14898 1.0
14899 1.0
14900 1.0
14901 1.0
[14902 rows x 1 columns]
据我了解,您需要读取 txt
个文件并将第一列写入 csv
个文件。对于您的用例,我编写了函数来读取 csv、提取第一列、组合数据的所有第一列并将它们写入最后的 csv
文件。为此,您需要指定数据文件的文件名。
def read_csv(file_path):
with open(file_path, "r") as f:
lines = f.readlines()
return lines[1:len(lines)-1] #ignoring your first line and last line
def extract_first_column(file_path): #returns first column
lines = read_csv(file_path)
first_col = []
for elem in lines:
elem = elem.strip().split(" ")
if not elem == "":
first_col.append(elem[0])
return first_col
def combined_array(file_path_list): #combines all of columns to one array
all_values = []
for file_path in file_path_list:
col_data = extract_first_column(file_path)
all_values.append(col_data)
return all_values
def write_csv(array, csv_name, delim): #writes the array to a csv file
with open(csv_name,"w") as f:
for elem in array:
if not elem == "":
f.write(elem + delim)
file_name_list = ["data.txt"] #you specify all 7 filenames here
array = combined_array(file_name_list) #array containing all 7 columns
array = [x for sublist in array for x in sublist]
write_csv(array, "test.csv", "\n") #writing to csv file
你提供的示例文件对我有用。如果我误解了你的要求,我会更新我的答案。