I have .TX0 file [some sort of csv txt file] and have converted this to a .txt file via python .readlines[], open[filename, 'w'] etc method. I have this new saved txt file but when i try to convert it to a dataframe it's giving me only one column. the txt file is below :
Empty DataFrame
Columns: [ '"Software Version:", 6.3.2.0646, Date:, 19/08/2015 09:26:04\n', '"Reprocess Number:", vma2: 261519, Unnamed: 7, \n', '"Sample Name:", , Data Acquisition Time:, 18/08/2015 17:23:23\n', '"Instrument Name:", natural gas [PE ASXL-TCD/FID], Channel:, B\n', '"Rack/Vial:", 0, 0.1, Operator:, joey.walker\n', '"Sample Amount:", 1.000000, Dilution Factor:, 1.000000\n', '"Cycle:", 1, Result File :, \\\\vma2\\TotalChrom\11170_he_tcd001.rst \n', '"Sequence File :", \\\\vma\C1_C2_binary.seq \n', '"===================================================================================================================================="\n', '""\n', '""\n'.1, '"condensate analysis [HP4890 Optic - FID]"\n', '"Peak", Component, Time, Area, Height, BL\n', '"#", Name, [min], [uV*sec], [uV], \n'.1, '------, ------, ------.1, ------.2, ------.3, ------\n', '1, Unnamed: 55, 0.810, 706.42, 304.38, *BB\n', '2, CH4, 0.900, 1113518.24, 495918.41, *BB\n'.1, '3, C2H6, 1.373, 901670.23, 295381.12, *BB\n'.2, '"", Unnamed: 73, Unnamed: 74, ------.4, ------.5, \n'.2, '"".1, Unnamed: 79, Unnamed: 80, 2015894.89, 791603.91, \n'.3, '"Missing Component Report"\n', '"Component", Expected Retention [Calibration File]\n', '------.1, ------\n'.1, '"All components were found"\n', '"Report stored in ASCII file :", C:\\Shared Folders\\TotalChrom\\11170_he_tcd001.TX0 \n']]
Index: []
for easier reading:
Empty DataFrame
Columns: [ '"Software Version:", 6.3.2.0646, Date:, 19/08/2015 09:26:04\n', '"Reprocess Number:", vma2: 261519, Unnamed: 7, \n', '"Sample Name:", , Data Acquisition Time:, 18/08/2015 17:23:23\n', '"Instrument Name:", natural gas [PE ASXL-TCD/FID], Channel:, B\n', '"Rack/Vial:", 0, 0.1, Operator:, joey.walker\n', '"Sample Amount:", 1.000000, Dilution Factor:, 1.000000\n', '"Cycle:", 1, Result File :, \\vma2\TotalChrom\data\Joey\Binary_Mixtures\Std1\11170_he_tcd001.rst \n', '"Sequence File :", \\vma2\TotalChrom\sequences\Joey\C1_C2_binary.seq \n', '"===================================================================================================================================="\n', '""\n', '""\n'.1, '"condensate analysis [HP4890 Optic - FID]"\n', '"Peak", Component, Time, Area, Height, BL\n', '"#", Name, [min], [uV*sec], [uV], \n'.1, '------, ------, ------.1, ------.2, ------.3, ------\n', '1, Unnamed: 55, 0.810, 706.42, 304.38, *BB\n', '2, CH4, 0.900, 1113518.24, 495918.41, *BB\n'.1, '3, C2H6, 1.373, 901670.23, 295381.12, *BB\n'.2, '"", Unnamed: 73, Unnamed: 74, ------.4, ------.5, \n'.2, '"".1, Unnamed: 79, Unnamed: 80, 2015894.89, 791603.91, \n'.3, '"Missing Component Report"\n', '"Component", Expected Retention [Calibration File]\n', '------.1, ------\n'.1, '"All components were found"\n', '"Report stored in ASCII file :", C:\Shared Folders\TotalChrom\data\Joey\Binary_Mixtures\Std1\11170_he_tcd001.TX0 \n']] Index: []
As you can see this is comma separated. Would there be any way of transferring this text to a comma delimited dataframe?
Thanks.
J
Created: March-19, 2020 | Updated: December-10, 2020 We will introduce the methods to load the data from a First, we will create a simple text file called We need to save it to the same directory from where Python script will be running. Example Codes: Output: We set read_csv[]
Method to Load Data From Text Fileread_fwf[]
Method to Load Width-Formated Text File to Pandas DataFrame
read_table[]
Method to Load Text File to Pandas DataFrame
txt
file with Pandas DataFrame
. We will also go through the available options.sample.txt
and add
the following lines to the file:45 apple orange banana mango
12 orange kiwi onion tomato
read_csv[]
Method to Load Data From Text Fileread_csv[]
is the best way to convert the text
file into Pandas DataFrame
. We need to set header=None
as we don’t have any header
in the above-created file. We can
also set keep_default_na=False
inside the method if we wish to replace empty values with NaN
.# python 3.x
import pandas as pd
df = pd.read_csv[
'sample.txt', sep=" ",header=None]
print[df]
0 1 2 3 4
0 45 apple orange banana mango
1 12 orange kiwi onion tomato
sep=" "
because a single white space separates values. Similarly, we can set sep=","
if we read data from a comma-separated file. Replace the white spaces inside sample.txt
with ,
and then run the code after replacing sep=" "
with sep=","
.
Sample.txt
45,apple,orange,banana,mango
12,orange,kiwi,,tomato
Code:
# python 3.x
import pandas as pd
df = pd.read_csv[
'sample.txt', sep=",",header=None]
print[df]
Output:
0 1 2 3 4
0 45 apple orange banana mango
1 12 orange kiwi NaN tomato
read_fwf[]
Method to Load Width-Formated Text File to Pandas DataFrame
read_fwf[]
is very helpful to load a width-formatted text file. We can’t use sep
because different values may have different delimiters. Consider the following text file:
Sample.txt
45 apple orange banana mango
12 orange kiwi onion tomato
In Sample.text
, delimiter
is not the same for all values. So read_fwf[]
will do the job here.
Code:
# python 3.x
import pandas as pd
df = pd.read_fwf[
'sample.txt',header=None]
print[df]
Output:
0 1 2 3 4
0 45 apple orange banana mango
1 12 orange kiwi onion tomato
read_table[]
Method to Load Text File to Pandas DataFrame
read_table[]
is another approach to load data from text
file to Pandas DataFrame
.
Sample.txt:
45 apple orange banana mango
12 orange kiwi onion tomato
Code:
# python 3.x
import pandas as pd
df = pd.read_table[
'sample.txt',header=None,sep=" "]
print[df]
Output:
0 1 2 3 4
0 45 apple orange banana mango
1 12 orange kiwi onion tomato
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.