Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concat Strings
#1
Hi all,

Trying to concat this variable with strings but it is addind a line in output:
>>> del v_first_cell_name
>>> v_first_cell_name=subprocess.check_output("cat /tmp/p_f | sort -u | head -1", shell=True);
>>> v_first_cell_name=("physicaldisks_"+v_first_cell_name+".xml")
>>> print(v_first_cell_name)
physicaldisks_CELADM04
.xml
>>>
How to not ADD the ".xml" as new line, I mean, I want to add in same like to stay like physicaldisks_CELADM04.xml
Reply
#2
Guess this is not related to your problem. But I have seen this using subprocess on Windows:

You are not concatenating strings. You are concatenating bytes and strings. You need to convert the bytes object returned by the subprocess call to a str.
Output:
>>> v_first_cell_name = subprocess.check_output("TYPE data.csv", shell=True) >>> print(v_first_cell_name) b'this_is_a_test' #<- See! This is a bytes object, not str >>> v_first_cell_name = v_first_cell_name.decode() # <- Convert bytes to str >>> print(v_first_cell_name) this_is_a_test #<- Now it is a str. no b'' >>> print("some prefix" + v_first_cell_name + "some suffix") some prefixthis_is_a_testsome suffix
Reply
#3
Use .rstrip() to remove possible white space, including newline at the end
>>> 'CELADM04\n'.rstrip()
'CELADM04'
>>> 
Reply
#4
Is getoutput() returning bytes instead of a str a windows thing? Or is it related to the command you execute?
Reply
#5
It seems to return bytes in linux too, but I don't know where it is documented.
Reply
#6
@paulo79 You most tell that for some reason still use Python 2.7💀
All answer you get here is for Python 3.
So deanhystad dos not info and answers for Python 3.
Python 2.7 did not return a bytes object as Python 3 dos.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 777 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  DataFRame.concat() nafshar 3 792 Jul-14-2023, 04:41 PM
Last Post: nafshar
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,690 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  [SOLVED] Concat data from dictionary? Winfried 4 1,738 Mar-30-2022, 02:55 PM
Last Post: Winfried
  Splitting strings in list of strings jesse68 3 1,780 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  pd.concat Problem WiPi 1 1,768 May-27-2020, 07:42 AM
Last Post: WiPi
  Sqlite CONCAT columns issac_n 4 5,105 Mar-22-2020, 09:31 AM
Last Post: buran
  Concat multiple Excel sheets with exclusion alessandrotk 1 2,856 Jan-10-2020, 04:43 AM
Last Post: sandeep_ganga
  Finding multiple strings between the two same strings Slither 1 2,530 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,242 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020