How to format columns in a .txt file? - excel

I exported a list of data in columns from excel into a .txt file, but I'm having a hard time figuring out how to get all the columns to line up. Right now, the data looks like this:
C5H12N+ 8.609640E+01 1.30E+04 8.6096970E+01 -5.70E-04
C4H11N2+ 8.709170E+01 7.20E+02 8.7092215E+01 -5.15E-04
C3H10N3+ 8.808690E+01 1.10E+03 8.8087460E+01 -5.60E-04
C5H14N+ 8.811210E+01 2.90E+03 8.8112620E+01 -5.20E-04
C4H13N2+ 8.910730E+01 2.30E+02 8.9107865E+01 -5.65E-04
C6H8N+ 9.406510E+01 2.70E+02 9.4065670E+01 -5.70E-04
C5H7N2+ 9.506040E+01 1.60E+02 9.5060915E+01 -5.15E-04
With the longer chemical formulas, the numbers following don't line up. How can I fix this?

Output will not line up nicely in a text file. However, you could try modifying your excel columns before exporting as csv. Are you okay with padding values with spaces?
For example, changing "1.30E+04" to "1.30E+04 spacespacespaceetc"?
Here's a formula that would make each cell 20-width by padding with spaces until reaching 20, and will also trim at 20. Use for every cell.
=LEFT(B2 & REPT(" ",20),20)
This is how it would appear in a text editor:

Related

How to specify long body in the excel mailto formula in a CSV file?

Here is my CSV file:
Email,Values
"=HYPERLINK(CONCATENATE(""mailto:user#xyz.com?subject=X&body="", SUBSTITUTE(B2,CHAR(10),""%0d%0a"")), ""Send email"")","11231112311123111231
14562112311123111231
78292112311123111231
01233112311123111231
34543112311123111231
67834112311123111231
90154112311123111231
23465112311123111231
56765112311123111231
89066112311123111231
12376112311123111231
23487112311123111231"
When I open it in Excel I get this:
The problem is the length of the text in B2. Exactly the same formula with shorter content in B2 works fine:
Given that I must use a CSV file, does this issue have a solution?
Looks like the ("") double quotes are causing the issue. try this
=HYPERLINK(CONCATENATE("mailto:user#xyz.com?subject=X&body=", SUBSTITUTE(B2,CHAR(10),"%0d%0a")), "Send email")","11231112311123111231
14562112311123111231
78292112311123111231
01233112311123111231
34543112311123111231
67834112311123111231
90154112311123111231
23465112311123111231
56765112311123111231
89066112311123111231
12376112311123111231
23487112311123111231"

avoid "_X000D" due to line breaks

when copied multiline text from texttbox of an userform to an excel cell something like x000d is getting added at the end of each line.when i googled it i understood it is due to line break .but line break is really necessary for me.this
cells(i,"A").value = textbox1.value
i would like to get multi-line data in cell without "x000d "

Removing unwanted data from text file

I have a large text file exported from an application that has three unwanted zeros in each row. The text file needs to be imported into another application and the zeros cause a problem.
Basically the unwanted three zeros per row need to be deleted. These zeros are always in the same location (same number of characters when counting from the left), but the location is somewhere in the middle. I have tried various things like importing the file into excel, removing the zeroes and then exporting as text file, but always have formatting problems with the exported text file.
Can someone suggest a solution or point me in the right direction?
something like this ? (quickly done)
Sub replaceInTx()
Dim inFile As String, outFile As String
Dim curLine As String
inFile = "x:\Documents\test.txt"
outFile = inFile & ".new.txt"
Open inFile For Input As #1
Open outFile For Output As #2
Do Until EOF(1)
Line Input #1, curLine
Print #2, Replace(curLine, "000", "", 6, 1, vbTextCompare)
Loop
Close #1
Close #2
End Sub
Alternatively, you can do that with any text editor that allows block selection (I like Notepad2, tiny, fast and portable)
I see you use excel a lot.
When you import the text file into excel do you use the import function and do you push the data into separate cells?
if the cell is numeric you could do the following:
=LEFT(TEXT(G5,"#"),LEN(TEXT(G5,"#"))-3)
if the cell is text:
=LEFT(G5,LEN(G5)-3)
G5 would the cell the data row/field is in.
curLine = Left(curLine, 104)
This will take the first 104 characters

Adding a newline character within a cell (CSV)

I would like to import product descriptions that need to be logically broken according by things like description, dimensions, finishes etc. How can I insert a line break so that when I import the file they will show up?
This question was answered well at Can you encode CR/LF in into CSV files?.
Consider also reverse engineering multiple lines in Excel. To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a .csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.
I struggled with this as well but heres the solution. If you add " before and at the end of the csv string you are trying to display, it will consolidate them into 1 cell while honoring new line.
csvString += "\""+"Date Generated: \n" ;
csvString += "Doctor: " + "\n"+"\"" + "\n";
I have the same issue, when I try to export the content of email to csv and still keep it break line when importing to excel.
I export the conent as this: ="Line 1"&CHAR(10)&"Line 2"
When I import it to excel(google), excel understand it as string. It still not break new line.
We need to trigger excel to treat it as formula by:
Format -> Number | Scientific.
This is not the good way but it resolve my issue.
supposing you have a text variable containing:
const text = 'wonderful text with \n newline'
the newline in the csv file is correctly interpreted having enclosed the string with double quotes and spaces
'" ' + text + ' "'
On Excel for Mac 2011, the newline had to be a \r instead of an \n
So
"\"first line\rsecond line\""
would show up as a cell with 2 lines
I was concatenating the variable and adding multiple items in same row. so below code work for me. "\n" new line code is mandatory to add first and last of each line if you will add it on last only it will append last 1-2 character to new lines.
$itemCode = '';
foreach($returnData['repairdetail'] as $checkkey=>$repairDetailData){
if($checkkey >0){
$itemCode .= "\n".trim(#$repairDetailData['ItemMaster']->Item_Code)."\n";
}else{
$itemCode .= "\n".trim(#$repairDetailData['ItemMaster']->Item_Code)."\n";
}
$repairDetaile[]= array(
$itemCode,
)
}
// pass all array to here
foreach ($repairDetaile as $csvData) {
fputcsv($csv_file,$csvData,',','"');
}
fclose($csv_file);
I converted a pandas DataFrame to a csv string using DataFrame.to_csv() and then I looked at the results. It included \r\n as the end of line character(s). I suggest inserting these into your csv string as your row separation.
Depending on the tools used to generate the csv string you may need escape the \ character (\r\n).

Reading strings into Matlab from excel?

I would like to read strings into Matlab from an excel file
ID = xlsread('data.xlsx',1, 'D2:D4')
the cells in range D2:D4 have strings in them. When I try to import the strings into Matlab all I get is an empty list? what can I do to fix this?
If you're in Matlab 2010 you can also do something like this to avoid having extra values in your workspace.
[~, ~, raw] = xlsread('data.xlsx',1, 'D2:D4')
I need to use this
[num, txt, raw] = xlsread('data.xlsx',1, 'D2:D4')
the txt will import stings into Matlab.

Resources