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

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")))

Related

How do you maintain whitespace formatting within CSV column to be expressed with Selenium send_keys?

Python 3.7
Windows
CSV row data looks like this.
data,data,data,some text\n some {0} more data\n even more data\n,data
How do you keep the newlines and use format when using selenium?
payloads = []
with open(filepath,) as _file:
dgroups = csv.reader(_file, delimiter=',' )
bpost = {
'name':dgroups[1],
'text':dgroups[3],
}
...
#Selenium section to send the formated text to the browser.
Textbox.send_keys(payloads[i]['text'].format( payloads[i]['name'])
Expected
some text
some MYNAME more data
even more data
Actual
some text\n some {0} more data\n even more data\n
This is happening because the csv.reader function is reading in the strings with the backslash being escaped as double backslash:
"some text\\n some {0} more data\\n even more data\\n"
To solve this issue, you can do the following to made sure newlines are created correctly:
Textbox.send_keys(payloads[i]['text'].replace("\\n", "\n").format( payloads[i]['name'])

Remove double quotes while printing string in dataframe to text file

I have a dataframe which contains one column with multiple strings. Here is what the data looks like:
Value
EU-1050-22345,201908 XYZ DETAILS, CD_123_123;CD_123_124,2;1
There are almost 100,000 such rows in the dataframe. I want to write this data into a text file.
For this, I tried the following:
df.to_csv(filename, header=None,index=None,mode='a')
But I am getting the entire string in quotes when I do this. The output I obtain is:
"EU-1050-22345,201908 XYZ DETAILS, CD_123_123;CD_123_124,2;1"
But what I want is:
EU-1050-22345,201908 XYZ DETAILS, CD_123_123;CD_123_124,2;1 -> No Quotes
I also tried this:
df.to_csv(filename,header=None,index=None,mode='a',
quoting=csv.QUOTE_NONE)
However, I get an error that an escapechar is required. If i add escapechar='/' into the code, I get '/' in multiple places (but no quotes). I don't want the '/' either.
Is there anyway I can remove the quotes while writing into a text file WITHOUT adding any other escape characters ?
Based on OP's comment, I believe the semicolon is messing things up. I no longer have unwanted \ if using tabs to delimit csv.
import pandas as pd
import csv
df = pd.DataFrame(columns=['col'])
df.loc[0] = "EU-1050-22345,201908 XYZ DETAILS, CD_123_123;CD_123_124,2;1"
df.to_csv("out.csv", sep="\t", quoting=csv.QUOTE_NONE, quotechar="", escapechar="")
Original Answer:
According to this answer, you need to specify escapechar="\\" to use csv.QUOTE_NONE.
Have you tried:
df.to_csv("out.csv", sep=",", quoting=csv.QUOTE_NONE, quotechar="", escapechar="\\")
I was able to write a df to a csv using a single space as the separator and get the "quotes" around strings removed by replacing existing in-string spaces in the dataframe with non-breaking spaces before I wrote it as as csv.
df = df.applymap(lambda x: str(x).replace(' ', u"\u00A0"))
df.to_csv(outpath+filename, header=True, index=None, sep=' ', mode='a')
I couldn't use a tab delimited file for what I was writing output for, though that solution also works using additional keywords to df.to_csv(): quoting=csv.QUOTE_NONE, quotechar="", escapechar="")

How to split a csv line that has commas for formatting numbers

I download a cvs file ussing request and when I need to split but it has some formatting commas in the numbers fields, like:
line='2019-07-05,sitename.com,"14,740","14,559","7,792",$11.47'
when I try to splits:
data = line.split(',')
it got this value:
['2019-07-05', 'nacion.com', '"14', '740"', '"14', '559"',
'"7','792"', '$11.47']
I would need:
['2019-07-05', 'nacion.com', '14740', '14559', '7792', '$11.47']
I need to solve it in python 3.7
any help is welcome
I usually don't like using regex but there may be no other option here. Try this - it basically removes the inside ,s in two steps:
import re
line='2019-07-05,sitename.com,"14,740","14,559","7,792",$11.47'
new_line = re.sub(r',(?!\d)', r"xxx", line).replace(',','').replace('xxx',',')
print(new_line)
Output
2019-07-05,sitename.com,"14740","14559","7792",$11.47
You can now use:
data = new_line.split(',')
Explanation:
The regex ,(?!\d) selects all ,s in line that are not between two digits. The .sub replaces those (temporarily) with xxxs. The next .replace deletes the remaining ,s which are inside numbers by replacing them with nothing and, finally, the last .replace restores the , delimiters by replacing the temporary xxxs with ,.

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")

Julia: comparing strings with special characters

I need to read a text file which contains csv data with headers separating individual blocks of data. The headers always start with the dollar sign $. So my text file looks like:
$Header1
2
1,2,3,4
2,4,5,8
$Header2
2
1,1,0,19,9,8
2,1,0,18,8,7
What I want to do is if the program reaches to $Header2, I want to read all the next lines following it till it reaches, say, $Header3 or end of the file. I think I can use `cmp' in Julia for this. I tried with a small file that contains following text:
# file julia.txt
Julia
$Julia
and my code reads:
# test.jl
fname = "julia.txt"
# set some string values
str1 ="Julia";
str2 ="\$Julia";
# print the strings and check the length
println(length(str1),",",str1);
println(length(str2),",",str2);
# now read the text file to check if you are able to find the strings
# str1 and str2 above
println ("Reading file...");
for ln in eachline(fname)
println(length(ln),",",ln);
if (cmp(str1,ln)==0)
println("Julia match")
end
if (cmp(str2,ln)==0)
println("\$Julia match")
end
end
what I get as output from the above code is:
5,Julia
6,$Julia
Reading file...
6,Julia
7,$Julia
I don't understand why I get character length of 6 for string Julia and 7 for the string $Julia when they are read from the file. I checked the text file by turning on white spaces and there are none. What am i doing wrong?
The issue is that the strings returned by eachline contain a newline character at the end.
You can use chomp to remove it:
julia> first(eachline("julia.txt"))
"Julia\n"
julia> chomp(first(eachline("julia.txt")))
"Julia"
Also, you can simply use == instead of cmp to test whether two strings are equal. Both use a ccall to memcmp but == only does that for strings of equal length and is thus probably faster.

Resources