Search code examples
python-3.xexport-to-csv

Is there a way of writing txt columns to a csv file?


So I have 8 different .txt files that I want to take 1 data column from and create a csv file of all those data columns. I used this to get the column I want:

china_data = pd.read_csv('China results.txt', header=None, usecols=[0], sep='\t')

But I can't find anywhere that explains how to add these columns of data to a singular csv folder.

I've seen the

f.open("filename", "w")

But not sure if I can use that for what I'm trying to do.

Edit: the files I'm merging have this format,

             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]

Solution

  • From what I have understood, you need to read txt files and write the first column to a csv file. For your usecase, I wrote functions to read csv, extract first column, combine all of first columns of your data and write them to a csv file at the end. For that you need to specify filenames of your data files.

    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
    

    This worked for me with the sample file you provided. I will update my answer if I misunderstood your request.