简体   繁体   中英

How to print a list without brackets and commas

I have a file,

6802496011442316593 1625090609  51048468525236  aaa=1111|bbbb=15|cccc=216|dddd=1625090604|eeee=5|ffff=12000|ggg=brtnrn=|hhhh=4|ii=lfp|ts=1625090609
6802496011442316593 1625090609  51048468525236  aaa=1111|bbbb=15|cccc=216|dddd=1625090604|eeee=5|ffff=12000|ggg=brtnrn=|hhhh=4|ii=lfp|ts=1625090489
6802496011442316593 1625090609  51048468525236  aaa=1111|bbbb=15|cccc=216|dddd=1625090604|eeee=5|ffff=12000|ggg=brtnrn=|hhhh=4|ii=lfp|ts=1625090549
6802496011442316593 1625090609  51048468525236  aaa=1111|bbbb=15|cccc=216|dddd=1625090604|eeee=5|ffff=12000|ggg=brtnrn=|hhhh=4|ii=lfp|ts=1625090599
6802496011442316593 1625090609  51048468525236  aaa=1111|bbbb=15|cccc=216|dddd=1625090604|eeee=5|ffff=12000|ggg=brtnrn=|hhhh=4|ii=lfp|ts=1625090599

from which I am extracting the last element "ts=1625090609" without "ts=":

with open(inputt, "r") as f1:
    for line in f1:
        exp=(line.split("\t")[3])
        params=(exp.split("|"))
        extraparamts=list()
        for param in params:
            if "ts=" in param:
                extraparamts.append(param[3:-1])
        print(extraparamts)   

to list:

['1625090429']
['1625090489']
['1625090549']
['1625090599']
['1625090599']

and I want to print it in output without bracket and commas and in separate lines, like this:

1625090429
1625090489
1625090549
1625090599
1625090599

just to make it easier to sort and compare with same, but not sort file. Unfortunately it seems that

print(*tslist, sep=",")   

does not work for me. Can you please tell me what am I doing wrong? I have tried itertools and

尝试:

print(*tslist[0].splitlines(), sep="\n")   

Editing answer as per your edits , have added regex

import re

extraparamts = []
with open(inputt, "r") as f1:  
    f1 = f1.read()
    for line in f1.splitlines():  # you can ignore splitlines if your data does not require it
        if "ts" in line:
            matches = re.findall("ts.*", line)
            extraparamts.append(str(matches)[5:-5])
            
    

for data in extraparamts:
    print(data)

Will Give

1625090
1625090
1625090
1625090
1625090

for lists

Try this:

list = ['1625090429','1625090489','1625090549','1625090599','1625090599']
for item in list:
    print(item)

Output:

1625090429
1625090489
1625090549
1625090599
1625090599

for lists in lists

Try this:

list = [['1625090429'],['1625090489'],['1625090549'],['1625090599'],['1625090599']]
for item in list:
    for x in item:
        print(x)

Output:

1625090429
1625090489
1625090549
1625090599
1625090599

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM