Trouble writing a header line with a comma to an Excel csv file - excel

I'm trying to write a simple header line in Intel Fortran (containing actual content commas) to an Excel csv. What I'd like to see in the first two columns is:
FMG(1,1) FMG(2,1)
Enclosing each term in quotes "FGM(i,j)" worked when I did it line by line:
Code: write (*,*) "FMG(1,1), kg/s (O2): ", FMG(1,1)
Output: FMG(1,1), kg/s (O2): 0.129000000000000
Some of the things I've tried include:
code: write (10,*) "FMG(1,1)","FMG(2,1)"
csv column output: FMG(1 1)FMG(2 1)
code: write (10,*) "FMG(1,1)" , "FMG(2,1)"
csv column output: FMG(1 1)FMG(2 1) (same thing)
code: write (10,*) " FMG(1,1)," "FMG(2,1)"
csv column output: FMG(1 1) FMG(2,1)
got the 2nd one correctly

CSV by name means Comma Separated Values. If you output "FMG(1,1),FMG(1,2)" then removing the commas, you will get
FMG(1
1)
FMG(1
2)
which is what you are seeing. To include the commas, the strings need to be enclosed in quotes. If you write
write (10,*) '"FMG(1,1)","FMG(2,1)"'
it might achieve what you are looking for.

Related

Insert a next line between each designated delimiters in Python [duplicate]

This question already exists:
Restore the order of mismatched lines of CSV file in Python
Closed 1 year ago.
Given that the number of columns is 3, and the head of the data is correct, the column delimiter is by "<|>", the mismatched lines are due to accidental feed by a new line.
Consider the following CSV file,
PERSON_ID<|>DEPT_ID<|>DATE_JOINED
AAAAA<|>S1<|>2021/01
/03
BBBBBB<|>S2<|>2021/02/03
CCCCC<|>S1<|>2021/03/05
I wish the output like,
enter image description here
The first thing I did is to remove the white spacing in the CSV file.
import re
your_string ="""PERSON_ID<|>DEPT_ID<|>DATE_JOINED
AAAAA<|>S1<|>2021/01
/03
BBBBBB<|>S2<|>2021/02/03
CCCCC<|>S1<|>2021/03/05"""
print(re.sub(r'\s{1,}','',your_string.strip()))
After this step I get tape-like strings:
PERSON_ID<|>DEPT_ID<|>DATE_JOINEDAAAAA<|>S1<|>2021/01/03BBBBBB<|>S2<|>2021/02/03CCCCC<|>S1<|>2021/03/05
Now I need to feed in a correct next line in "2021/01/03BBBBBB".
Assuming the total number of columns is 3, so we need to feed the next line between each:
the 2nd delimiter to 3rd delimiter,
the 4th delimiter to 5th delimiter,
the 6th delimiter to 7th delimiter...and so on.
Assuming the date shown in the string at a fixed length of 10, so I need a new line spacing feed in each designated delimiter after a string length of 10.
Assuming the data head will not change, so I can insert a new line spacing after a string length of 33 from the beginning of the file.
Then, finally, I can get my correct data in lines, the output of the rows in CSV would be like,
PERSON_ID<|>DEPT_ID<|>DATE_JOINED
AAAAA<|>S1<|>2021/01/03
BBBBBB<|>S2<|>2021/02/03
CCCCC<|>S1<|>2021/03/05
After this, I can separate them by the string delimiters. Hence, complete the mismatched lines restoration.
Therefore, I need help on how to insert a next line between the designated delimiters at a string length of 10 from its beginning?
Thanks!
What about getting lines of fields directly? Like that:
sep = '<|>'
your_data = [line.strip().split(sep) for line in your_string.strip().split('\n') if sep in line]
You got:
[['PERSON_ID', 'DEPT_ID', 'DATE_JOINED'], ['AAAAA', 'S1', '2021/01'], ['BBBBBB', 'S2', '2021/02/03'], ['CCCCC', 'S1', '2021/03/05']]

Reading from file returns 2 dictionaries

data = [line.strip('\n') for line in file3]
# print(data)
data2 = [line.split(',') for line in data]
data_dictionary = {t[0]:t[1] for t in data2}
print(data_dictionary)
So I'm reading content from a file under the assumption that there is no whitespace at the beginning of each line and not blank lines anywhere.
when I read this file I first strip the newline character and the split the data by a ',' because that is what the data in the file is separated by. but when I make the dictionary it returns two dictionaries instead of one it's doing that for other files where I use this procedure. how do I fix this?

removing extra column in a csv file while exporting data using python3

I wrote a function in python3 which merges some files in the same directory and returns a csv file as the output but the problem with csv file is that I get one extra column at the beginning which does not have header and the other rows of that columns are numbers starting from 0. do you know how I write the csv file without getting the extra column?
you can split by ,, and then use slicing to remove the first element.
example:
original = """col1,col2,col3
0,val01,val02,val03
1,val11,val12,val13
2,val21,val22,val23
"""
original_lines = original.splitlines()
result = original_lines[:1] # copy header
for line in original_lines[1:]:
result.append(','.join(line.split(',')[1:]))
print('\n'.join(result))
Output:
col1,col2,col3
val01,val02,val03
val11,val12,val13
val21,val22,val23

How can I write a string to binary file using Python3?

I would like to write strings to a bin file as header.
However, I can only write type 'bytes' to the binary file.
Here is my code:
header1 = str.encode("1\n")
header1 = str.encode("2\n")
print (type(header))
with open("abc.bin",'wb') as f_test:
f_test.write(header1)
f_test.write(header2)
Here are my questions:
1, when I open the abc.bin file using notepad, I can see "1" and "2" but they are not at the separated line. Why is it seems that \n is not functional?
2, in the .bin file, what are the format of "1" and "2". are they strings?
3, I tried pickle and marshal too. However, when I open .bin file, I found something in front of "1" and "2"(like when I used marshal.dump(header1,f_test), it gave me: ?1?2). What are these'?' and where do they come frome?
This is not originally from me, but I get the solution from the comment on this post:
https://pythonconquerstheuniverse.wordpress.com/2011/05/08/newline-conversion-in-python-3/
To Sum up, the newline need to be converted to a byte. i.e. b"\n"
if you try the following, it will print a new line:
header1 = str.encode("1")
header1 = str.encode("2")
print (type(header))
with open("abc.bin",'wb') as f_test:
f_test.write(header1+b"n")
f_test.write(header2+b"n")

python 3 how to avoid print a newline after "."

I have this code:
f=open('myfile.txt','r')
name=[]
for line in f:
name.append(line)
for i in range (len(name)):
print("hola"+name[i]+".txt".format((name[i]).strip("\r\n")))
myfile has two rows separated by newline, like this:
Da
Df
And I would like this ouput:
holaDa.txt
holaDf.txt
But instead, I have this:
holaD4I5M4
.txt
holaD4i5J8.txt
And I tryed several things to avoid the newline before the ".", but nothing seems to work.
Thank for your help! I am very new in Python, sorry!
You're stripping the string correctly, but instead of outputting the stripped value, you're outputting the original value. Using using "hola{0}.txt" format instead of outputting the original value with "hola"+name[i]+".txt" will output the correct - stripped - string;
for i in range (len(name)):
print("hola{0}.txt".format((name[i]).strip("\r\n")))

Resources