Python - Print items in list with neither commas nor apostrophes

后端 未结 2 1895
萌比男神i
萌比男神i 2021-01-25 14:30

Minimal working example of my code:

# Create output data file
out_data_file = open(\'output_file\',\'w\')
out_data_file.write(\"# Header \\n\")
out_data_file.clo         


        
相关标签:
2条回答
  • 2021-01-25 14:56
    f.write('Text'+'  '+' '.join('%d' % item for item in list1))
    

    Or, since it looks like you are OK with the default conversion of an integer to string:

    f.write('Text'+'  '+' '.join(str(item) for item in list1))
    
    0 讨论(0)
  • 2021-01-25 15:06
    Use
    
    In [10]: list1 = [1,3,4,5,12,6,2,35,74,6,2]
    
    In [11]: " ".join(map(str, list1))
    Out[11]: '1 3 4 5 12 6 2 35 74 6 2'
    

    ...or in your case:

    f.write('Text' + ' ' + " ".join(map(str, list1)))
    
    0 讨论(0)
提交回复
热议问题