I've written some code, that analyses .txt file line by line, however it is written quite bad. Currently I have brain fog and my code has issue and my txt file needs blank line at the end in order to create proper last group. Without this line function sets name of last group the same as one before last, so that it causes further problems.
The txt file looks like this:
time in seconds
addr1,channel1
addr2,channel2
staticval1,channel3
staticval2,channel4
groupname1
addr3,channel5
staticval3,channel6
groupname2
etc
1
239.0.1.104,kanal1
233.51.52.170,kanal2
239.0.1.105,kanal3
1.0,kanal4
2.0,kanal5
grupa12
239.0.1.122,kanal6
239.0.1.116,kanal7
1.5,kanal8
4.5,kanal9
grupa123
239.0.1.119,kanal10
239.0.1.112,kanal11
4.5,KANAL112
4.0,KANAL123
1.5,KANAL134
grupa1234
and the code is:
with open("INPUT_DATA_SIMULATION.txt", "r") as filestream:
for line in islice(filestream, 0, 1):
scanTime = int(line)
for line in filestream:
currentline = line.split(",")
if not "grupa" in line:
#walk thorugh lines inbetween "groups"
if(currentline != ["\n"]):
for y in range(len(currentline[0])):
if(currentline[0][y] == "."):
valChecker += 1
if(valChecker>=2):
MCAST_ADDRESSES.append(currentline[0])
elif(valChecker<=1):
MCAST_VALUES.append(float(currentline[0]))
valChecker = 0
if("\n" in currentline[1]):
currentline[1] = currentline[1][:-1]
MCAST_NAMES.append(currentline[1])
else:
MCAST_NAMES.append(currentline[1])
#if blank line found break loop
else:
break
elif "grupa" in line:
groupName = line[:-1]
shouldAdd = True
if(shouldAdd):
MCAST_GRP.append(groupName)
MCAST_GRP.append(MCAST_ADDRESSES)
MCAST_GRP.append(MCAST_VALUES)
MCAST_GRP.append(MCAST_NAMES)
MCAST_GROUPS.append(MCAST_GRP)
MCAST_GRP = []
MCAST_ADDRESSES = []
MCAST_VALUES = []
MCAST_NAMES = []
shouldAdd = False
return MCAST_GROUPS, scanTime
How should I modify code in order to input format be:
time in seconds
groupname1
addr1,channel1
static1,channel2
groupname2
addr2,channel3
so the group name is before addreses and static values, not after? Also modifying part responsible for deleting "\n" at the end of the lines would be great.
Thank you in advance.