python 怎么去掉文本中每一行的首字符
2#definePORT253413#defineBACKLOG54#defineMEM_SIZE102456structevent_base*base;78intmai...
2 #define PORT 25341
3 #define BACKLOG 5
4 #define MEM_SIZE 1024
5
6 struct event_base* base;
7
8 int main(int argc, char* argv[])
9 {
txt中的内容如下,怎么去掉没一行的第一个字符
10 struct sockaddr_in my_addr;
11 int sock;
12
13 sock = socket(AF_INET, SOCK_STREAM, 0);
14 int yes = 1;
15 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
16 memset(&my_addr, 0, sizeof(my_addr));
17 my_addr.sin_family = AF_INET;
18 my_addr.sin_port = htons(PORT);
19 my_addr.sin_addr.s_addr = INADDR_ANY;
20 bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
21 listen(sock, BACKLOG);
22
23 struct event listen_ev;
24 base = event_base_new();
25 event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
26 event_base_set(base, &listen_ev);
27 event_add(&listen_ev, NULL);
28 event_base_dispatch(base);
29
30 return 0;
31 } 展开
3 #define BACKLOG 5
4 #define MEM_SIZE 1024
5
6 struct event_base* base;
7
8 int main(int argc, char* argv[])
9 {
txt中的内容如下,怎么去掉没一行的第一个字符
10 struct sockaddr_in my_addr;
11 int sock;
12
13 sock = socket(AF_INET, SOCK_STREAM, 0);
14 int yes = 1;
15 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
16 memset(&my_addr, 0, sizeof(my_addr));
17 my_addr.sin_family = AF_INET;
18 my_addr.sin_port = htons(PORT);
19 my_addr.sin_addr.s_addr = INADDR_ANY;
20 bind(sock, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
21 listen(sock, BACKLOG);
22
23 struct event listen_ev;
24 base = event_base_new();
25 event_set(&listen_ev, sock, EV_READ|EV_PERSIST, on_accept, NULL);
26 event_base_set(base, &listen_ev);
27 event_add(&listen_ev, NULL);
28 event_base_dispatch(base);
29
30 return 0;
31 } 展开
2个回答
展开全部
# 去掉首字符
txt = 'abcdefg'
print(txt[1:]) # bcdefg
# 去掉空格前字符
txt = '1 2345678'
print(txt.split()[1]) # 2345678
# 去掉每行首空格前字符
txt = '''
2 #define PORT 25341
3 #define BACKLOG 5
4 #define MEM_SIZE 1024
'''
arr = txt.strip().split('\n') # 用换行符分割成列表
print(arr) # ['2 #define PORT 25341', '3 #define BACKLOG 5', '4 #define MEM_SIZE 1024']
newArr = [] # 创建新列表
for v in arr:
varr = v.split() # 用空格分割成列表
newStr = v.replace(varr[0] + ' ', '') # 去掉第一个
newArr.append(newStr) # 添加到新列表
print(newArr) # ['#define PORT 25341', '#define BACKLOG 5', '#define MEM_SIZE 1024']
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询